Mac Quick C question (arrays)

beefstu01

macrumors member
Original poster
Oct 24, 2002
85
0
CA
Hey,

I was hoping someone here could help me. Is there any way for you to find the size of an array in C like you can in Java? Dunno if I explained it clearly, so here's what I'm talking about:

Java:
Code:
for(int i = 0; i < myArray.length; i++)
     (stuff)
Is there any way for me to implement that in C without knowing the length of the array beforehand?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,594
57
Harrogate
The problem is that straight C is not object-oriented. An array is simply a load of memory that you are happening to treat as an array. If you know what type of stuff is in the array the sizeof stuff can help. Another common practice is to terminate the array with a special character (Strings in C are arrays of characters with \0 termination).
 

beefstu01

macrumors member
Original poster
Oct 24, 2002
85
0
CA
I'll know exactly what I'll be operating on, so sizeof() will work for me. I'm making a function that takes in an integer array with random numbers in it, so I can't really have a termination character.

Thanks a lot.
 

savar

macrumors 68000
Jun 6, 2003
1,954
0
District of Columbia
beefstu01 said:
Hey,

I was hoping someone here could help me. Is there any way for you to find the size of an array in C like you can in Java? Dunno if I explained it clearly, so here's what I'm talking about:

Java:
Code:
for(int i = 0; i < myArray.length; i++)
     (stuff)
Is there any way for me to implement that in C without knowing the length of the array beforehand?
Contrary to what others have said, I don't think this is possible. An array in C is *not* an object, so how would it have any way to keep track of its own length?

I tested the following program:
Code:
#include <stdio.h>

main() {
 int testArray[] = {1, 2, 3, 4};
 printf("size test:%d\n",sizeof(testArray));
}
The answer I get is 16, which is 4 * 4 bytes per int. I'm guessing that the only reason I get this answer is because the array is statically initialized and so the compiler can treat is as a defined type.

In C, there are two ways to iterate through an array. One is to keep track of the length of the array, and pass that value to any code which needs it. The other is to terminate the array with a sentinel value: i.e. if your array stores positive ints, make -1 signal the end of the array.

This is why the signature for main() is

int main(int argc, char **argv)

Because if you want to loop through the arguments to your program, you need to know how many there are.
 

jcgerm

macrumors member
May 28, 2003
91
0
beefstu01 said:
I'll know exactly what I'll be operating on, so sizeof() will work for me. I'm making a function that takes in an integer array with random numbers in it, so I can't really have a termination character.

Thanks a lot.
Why can't you just keep track of the size of the array? You have to know the size of the array to create it, so just keep track of it that way. You can either dynamically allocate it or statically allocate it, but either way you have to know the size to create it at some point.

Jeremy