Number of elements is an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Oliphant

    Number of elements is an array

    Say I have a line in my code something like the following (NOTE: Point is
    __value class System::Drawing ::Point):

    Point point[] = new Point[3] ;

    How do I now 'ask' 'point' how many elements it has (in this case the answer
    is 3)?

    This does NOT work:

    int element_count = sizeof(point) ; // error C3608: can't apply sizeof to a
    __gc array

    Thanks in advance!!! : )

    [==P ==]


  • Tom Serface

    #2
    Re: Number of elements is an array

    Of course, an obvious solution is to do something like:

    #define POINT_ARRAY_SZ 3

    Point point[] = new Point[POINT_ARRAY_SZ];

    Then you could use it anywhere you want.

    Tom

    "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
    news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Say I have a line in my code something like the following (NOTE: Point is
    > __value class System::Drawing ::Point):
    >
    > Point point[] = new Point[3] ;
    >
    > How do I now 'ask' 'point' how many elements it has (in this case the
    > answer is 3)?
    >
    > This does NOT work:
    >
    > int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
    > a __gc array
    >
    > Thanks in advance!!! : )
    >
    > [==P ==]
    >[/color]


    Comment

    • James Park

      #3
      Re: Number of elements is an array

      point->Length

      "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
      news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=blue]
      > Say I have a line in my code something like the following (NOTE: Point is
      > __value class System::Drawing ::Point):
      >
      > Point point[] = new Point[3] ;
      >
      > How do I now 'ask' 'point' how many elements it has (in this case the
      > answer is 3)?
      >
      > This does NOT work:
      >
      > int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
      > a __gc array
      >
      > Thanks in advance!!! : )
      >
      > [==P ==]
      >[/color]


      Comment

      • Peter Oliphant

        #4
        Re: Number of elements is an array

        > #define POINT_ARRAY_SZ 3[color=blue]
        >
        > Point point[] = new Point[POINT_ARRAY_SZ];
        >
        > Then you could use it anywhere you want.[/color]

        Hi Tom,

        I see what you're saying, but what I need is the ability to find out the
        number of elements in an *arbitrary* array. Put another way, when the number
        of elements in the array is defined at runtime and is a variable.

        This is exactly what I want to be able to do:

        int Array_Size( Point point[] )
        {
        return number_of_eleme nts_in( point ) ;
        }

        To put this in context, I'm using the Graphics DrawPolygon method. Here it
        takes as a parameter Point[], the list of the vertices of the polygon. But
        the function does not take the number of elements in Point[] as a parameter,
        hence DrawPoygon() must be able to figure out the number of elements in
        Point[] just from Point[] (rather, an instance of). I need to be able to do
        the same since I must process these points before I give them to
        DrawPolygon, so of course I need to know the number of elements in Point[]
        as well from the same info (i.e., I'm not given the Point array size in
        number of elements).


        "Tom Serface" <tserface@msn.c om> wrote in message
        news:%23RjCNw02 FHA.2816@tk2msf tngp13.phx.gbl. ..[color=blue]
        > Of course, an obvious solution is to do something like:
        >
        > #define POINT_ARRAY_SZ 3
        >
        > Point point[] = new Point[POINT_ARRAY_SZ];
        >
        > Then you could use it anywhere you want.
        >
        > Tom
        >
        > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
        > news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=green]
        >> Say I have a line in my code something like the following (NOTE: Point is
        >> __value class System::Drawing ::Point):
        >>
        >> Point point[] = new Point[3] ;
        >>
        >> How do I now 'ask' 'point' how many elements it has (in this case the
        >> answer is 3)?
        >>
        >> This does NOT work:
        >>
        >> int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
        >> a __gc array
        >>
        >> Thanks in advance!!! : )
        >>
        >> [==P ==]
        >>[/color]
        >
        >[/color]


        Comment

        • Peter Oliphant

          #5
          Re: Number of elements is an array

          Hi James,

          Unfortunately points->Length doesn't work, but instead generates the error:

          C2039: 'Length' is not a member of 'System::Drawin g::Point'.

          And I've tried 'length', 'size', and 'Size' as well, with no luck.

          There HAS to be way to get the number of elements in a Point[] array, but
          what is it?

          [==P==]

          "James Park" <someone@hotmai l.com> wrote in message
          news:OicdkI12FH A.3296@TK2MSFTN GP09.phx.gbl...[color=blue]
          > point->Length
          >
          > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
          > news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=green]
          >> Say I have a line in my code something like the following (NOTE: Point is
          >> __value class System::Drawing ::Point):
          >>
          >> Point point[] = new Point[3] ;
          >>
          >> How do I now 'ask' 'point' how many elements it has (in this case the
          >> answer is 3)?
          >>
          >> This does NOT work:
          >>
          >> int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
          >> a __gc array
          >>
          >> Thanks in advance!!! : )
          >>
          >> [==P ==]
          >>[/color]
          >
          >[/color]


          Comment

          • Peter Oliphant

            #6
            Re: Number of elements is an array

            My apologies, I was using it wrong. point->Length DOES work. Thanks! : )

            [==P=]]

            "Tom Serface" <tserface@msn.c om> wrote in message
            news:%23RjCNw02 FHA.2816@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Of course, an obvious solution is to do something like:
            >
            > #define POINT_ARRAY_SZ 3
            >
            > Point point[] = new Point[POINT_ARRAY_SZ];
            >
            > Then you could use it anywhere you want.
            >
            > Tom
            >
            > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
            > news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=green]
            >> Say I have a line in my code something like the following (NOTE: Point is
            >> __value class System::Drawing ::Point):
            >>
            >> Point point[] = new Point[3] ;
            >>
            >> How do I now 'ask' 'point' how many elements it has (in this case the
            >> answer is 3)?
            >>
            >> This does NOT work:
            >>
            >> int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
            >> a __gc array
            >>
            >> Thanks in advance!!! : )
            >>
            >> [==P ==]
            >>[/color]
            >
            >[/color]


            Comment

            • Peter Oliphant

              #7
              Re: Number of elements is an array

              My apologies, I was using it wrong. point->Length DOES work. Thanks! : )

              [==P=]

              "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
              news:%238tXbu82 FHA.3188@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > Hi James,
              >
              > Unfortunately points->Length doesn't work, but instead generates the
              > error:
              >
              > C2039: 'Length' is not a member of 'System::Drawin g::Point'.
              >
              > And I've tried 'length', 'size', and 'Size' as well, with no luck.
              >
              > There HAS to be way to get the number of elements in a Point[] array, but
              > what is it?
              >
              > [==P==]
              >
              > "James Park" <someone@hotmai l.com> wrote in message
              > news:OicdkI12FH A.3296@TK2MSFTN GP09.phx.gbl...[color=green]
              >> point->Length
              >>
              >> "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
              >> news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=darkred]
              >>> Say I have a line in my code something like the following (NOTE: Point
              >>> is __value class System::Drawing ::Point):
              >>>
              >>> Point point[] = new Point[3] ;
              >>>
              >>> How do I now 'ask' 'point' how many elements it has (in this case the
              >>> answer is 3)?
              >>>
              >>> This does NOT work:
              >>>
              >>> int element_count = sizeof(point) ; // error C3608: can't apply sizeof
              >>> to a __gc array
              >>>
              >>> Thanks in advance!!! : )
              >>>
              >>> [==P ==]
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Peter Oliphant

                #8
                Re: Number of elements is an array

                Ok, got another question. How do I return a Point[] (pointer) from a method?
                The following won't work:

                Point[] Get_Points() { return m_Points ; } // error: Point[] is not a valid
                syntax here

                where m_Points is defined by:

                Point m_Points[] ;

                I CAN return a pointer to the first element of the list, but I'm guessing
                this doesn't retain the array info (such as 'Length'....heh e). That is, I
                CAN do the following:

                Point* Get_Points() { return &(m_Points[0]) ; }

                Is there a way to return a pointer to an array of Point's? Will the above do
                the trick and preserve its interpretation of the return value as a pointer
                to an array, not just a single element (which I doubt, but it might know
                from context)?

                Thanks (again) in advance! : )

                [==P==]

                "James Park" <someone@hotmai l.com> wrote in message
                news:OicdkI12FH A.3296@TK2MSFTN GP09.phx.gbl...[color=blue]
                > point->Length
                >
                > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                > news:eQMlId02FH A.632@TK2MSFTNG P10.phx.gbl...[color=green]
                >> Say I have a line in my code something like the following (NOTE: Point is
                >> __value class System::Drawing ::Point):
                >>
                >> Point point[] = new Point[3] ;
                >>
                >> How do I now 'ask' 'point' how many elements it has (in this case the
                >> answer is 3)?
                >>
                >> This does NOT work:
                >>
                >> int element_count = sizeof(point) ; // error C3608: can't apply sizeof to
                >> a __gc array
                >>
                >> Thanks in advance!!! : )
                >>
                >> [==P ==]
                >>[/color]
                >
                >[/color]


                Comment

                • Arnaud Debaene

                  #9
                  Re: Number of elements is an array

                  Peter Oliphant wrote:[color=blue]
                  > Ok, got another question. How do I return a Point[] (pointer) from a
                  > method? The following won't work:
                  >
                  > Point[] Get_Points() { return m_Points ; } // error: Point[] is not a
                  > valid syntax here[/color]

                  Point Get_Points() []
                  { return m_Points ; }

                  Geee, *why* oh why did they choose to keep this awfull K&R syntax ;-(

                  Arnaud
                  MVP - VC


                  Comment

                  • Peter Oliphant

                    #10
                    Re: Number of elements is an array

                    > Point Get_Points() [][color=blue]
                    > { return m_Points ; }[/color]

                    I never in a million years would have tried that syntax...but it definitely
                    works...Thanks! !! : )

                    [==P==]

                    "Arnaud Debaene" <adebaene@clu b-internet.fr> wrote in message
                    news:uxTPpY%232 FHA.2640@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                    > Peter Oliphant wrote:[color=green]
                    >> Ok, got another question. How do I return a Point[] (pointer) from a
                    >> method? The following won't work:
                    >>
                    >> Point[] Get_Points() { return m_Points ; } // error: Point[] is not a
                    >> valid syntax here[/color]
                    >
                    > Point Get_Points() []
                    > { return m_Points ; }
                    >
                    > Geee, *why* oh why did they choose to keep this awfull K&R syntax ;-(
                    >
                    > Arnaud
                    > MVP - VC
                    >
                    >[/color]


                    Comment

                    • Tom Serface

                      #11
                      Re: Number of elements is an array

                      Sorry Peter, didn't understand your requirement. Length would be the way to
                      go in this case. I thought you were using a generic example.

                      Tom

                      "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                      news:ezPCls82FH A.1148@tk2msftn gp13.phx.gbl...[color=blue][color=green]
                      >> #define POINT_ARRAY_SZ 3
                      >>
                      >> Point point[] = new Point[POINT_ARRAY_SZ];
                      >>
                      >> Then you could use it anywhere you want.[/color]
                      >
                      > Hi Tom,[/color]


                      Comment

                      • Arnaud Debaene

                        #12
                        Re: Number of elements is an array

                        Peter Oliphant wrote:[color=blue][color=green]
                        >> Point Get_Points() []
                        >> { return m_Points ; }[/color]
                        >
                        > I never in a million years would have tried that syntax...[/color]

                        Well, RTFM ;-)


                        Arnaud
                        MVP - VC




                        Comment

                        Working...