Array length

R

Ronny Mandal

I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


Thanks.

Ronny Mandal
 
K

Karthik Kumar

Ronny said:
I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


Thanks.

Ronny Mandal


The answer is not possible. While dealing with 2-D arrays like that,
generally people tend to encapsulate them in a struct with the
dimensions in it and return a variable of that struct.
 
M

Michael Mair

Ronny said:
I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


If you are really talking about arrays of arrays of "type", consider:


#include <stdio.h>

int main (void)
{
int a[2][3];

printf("a[2][*]: %lu\na[*][3]: %lu\na[*][*]: %lu\n",
(unsigned long) (sizeof a[0]/sizeof a[0][0]),
(unsigned long) (sizeof a /sizeof a[0]),
(unsigned long) (sizeof a /sizeof a[0][0]));

return 0;
}

Note: With a C99 library, you can use the length modifier
z instead of l in the printf format string and do no longer
need the cast.

Apart from that, the FAQ section about arrays and pointers
may be helpful for you. Start from
http://www.eskimo.com/~scs/C-faq/top.html


Cheers
Michael
 
E

Erik de Castro Lopo

Ronny said:
I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


#define ARRAY_LEN(x) (sizeof(x) / sizeof ((x) [0]))

which can then be used as:

len1 = ARRAY_LEN (foo) ;
len2 = ARRAY_LEN (foo [0])

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
Open Source and Free Software means that you never sacrifice quality
of the code for meeting deadlines set up by people not participating
directly in the software development process.
 
L

Lawrence Kirby

Ronny said:
I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


#define ARRAY_LEN(x) (sizeof(x) / sizeof ((x) [0]))

which can then be used as:

len1 = ARRAY_LEN (foo) ;
len2 = ARRAY_LEN (foo [0])


But note that this only works when foo is the name of an array with a full
definition in scope. It doesn't work if foo is a pointer, at least at the
top level. Specifically this mechanism gets information from the TYPE of
foo, it doesn't look at the size of actual runtime objects (even with C99
VLAs it is still effectively a type thing). So given

int array[4][5];
int (*ptr)[5] = array;

I can use ARRAY_LEN(array) to get 4, ARRAY_LEN(array[0]) to get 5 and
ARRAY_LEN(ptr[0]) to get 5, because this is all information that is in the
types of array and ptr. However ARRAY_LEN(ptr) is nonsense because ptr
itself is a pointer not an array and provides no top level size
information (array, array[0] and ptr[0] are expressions with array type).
sizeof(ptr) provides information about the pointer, not the array it
points into.

C provides no way to get the number of elements of the top level array
from a pointer. If you need that information you have to deal with
that yourself, e.g. by recording it and passing it around.

Also be careful of function parameters:

void bar(int array[4][5])
{
size_t asize = ARRAY_LEN(array); /* Wrong */
size_t bsize = ARRAY_LEN(*array); /* Good */
}

Despite appearances array here is not an array, it is a pointer with type
int (*)[5]. So the ARRAY_LEN(array) expression is invalid as in the case
of ptr above.

Lawrence
 
J

Joe Wright

Ronny said:
I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?


Thanks.

Ronny Mandal

#include <stdio.h>

#define R 4
#define C 5

int main(void) {
char arr[R][C];
printf("Size of arr is %2d\n"
"Size of arr[0] is %2d\n"
"Size of arr[0][0] is %2d\n",
(int)sizeof arr,
(int)sizeof arr[0],
(int)sizeof arr[0][0]);
return 0;
}
 
B

Barry Schwarz

I could not find anything about this in the FAQ, so:

I have a two-dim array, foo[a] (a and b are arbitrary). How do I get C to
return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
the array?

You can get the value of a particular dimension by dividing the size
of the array without that dimension by the size of the array element.

In your example

sizeof foo / sizeof foo[0] will evaluate to a. Instead of foo[0]
you can use *foo.

sizeof foo[0] / sizeof *foo[0] will evaluate to b.

This will work regardless of the number of dimensions.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top