Number of elements is an array

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

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 ==]
 
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
 
#define POINT_ARRAY_SZ 3
Point point[] = new Point[POINT_ARRAY_SZ];

Then you could use it anywhere you want.

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_elements_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 said:
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 said:
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 ==]
 
Hi James,

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

C2039: 'Length' is not a member of 'System::Drawing::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 said:
point->Length

Peter Oliphant said:
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 ==]
 
My apologies, I was using it wrong. point->Length DOES work. Thanks! : )

[==P=]]

Tom Serface said:
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 said:
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 ==]
 
My apologies, I was using it wrong. point->Length DOES work. Thanks! : )

[==P=]

Peter Oliphant said:
Hi James,

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

C2039: 'Length' is not a member of 'System::Drawing::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 said:
point->Length

Peter Oliphant said:
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 ==]
 
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'....hehe). 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 said:
point->Length

Peter Oliphant said:
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 ==]
 
Peter said:
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

Point Get_Points() []
{ return m_Points ; }

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

Arnaud
MVP - VC
 
Point Get_Points() []
{ return m_Points ; }

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

[==P==]

Arnaud Debaene said:
Peter said:
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

Point Get_Points() []
{ return m_Points ; }

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

Arnaud
MVP - VC
 
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 said:
#define POINT_ARRAY_SZ 3

Point point[] = new Point[POINT_ARRAY_SZ];

Then you could use it anywhere you want.

Hi Tom,
 
Back
Top