# Thread: count elements in an array

1. ## count elements in an array

Hi, all!

do you know if there is any predefined function to count all the element in an array? or we have to write such functions ourselves?

for instance: if i have an array like this below:

Code:
`int intMyArray[]={1,2,3,4,6,7};`
how can we find out if there are 6 elements in the array intMyArray[] ??

usually, Iwrite

Code:
`count  = sizeof (intMyArray)/sizeof(int); // this work, it gives me correct value`
however, when I pass the array to a function like,

Code:
```#include <stdio.h>

int countArrayElement(int arr[]);

int main()
{
int intMyArray[]={1,2,3,4,6,7};
countArrayElement(intMyArray);
return 0;
}

int countArrayElement(int arr[])
{
int count = 0;
count  = sizeof (arr)/sizeof(int);  // wont work at all, arr is just the first element
return count;
}```

2. No, you have to keep track of the size yourself (when you pass by pointer).

Something like:
Code:
`int countArrayElement(int arr[], size_t elements)`
FYI, I like using,
Code:
`size = sizeof(array) / sizeof(array[0]);`
So if you change the type of array it's not going to mess up.

3. ## size_t????

Originally Posted by zacs7
No, you have to keep track of the size yourself (when you pass by pointer).

Something like:
Code:
`int countArrayElement(int arr[], size_t elements)`
FYI, I like using,
Code:
`size = sizeof(array) / sizeof(array[0]);`
So if you change the type of array it's not going to mess up.
Then, I have one question, why do we have to use size_t????, why don't we use int??
i just know that size_t can be the same as unsigned integer or unsigned long. I hope you can clarify this, since I have been so wondering since the time I start to learn C!

4. Because the elements of an array are unsigned (0 to whatever), they can't be negative. You don't have to use size_t, but it's good to. Read http://www.embedded.com/columns/prog...0900195?pgno=2

size_t can be implemented as signed, but then it'd be signed everywhere else (ie in malloc(), sizeof() etc).

5. size_t can be implemented as signed
size_t has to be an unsigned integer type.

6. I knew that, except the link I posted states otherwise (pre GCC 2.4), read it

7. That'd make those compilers non-standard compliant?
Yes, at least according to my copy of C99. I wonder why those compilers have it as signed in the first place, hmm...

8. I don't think they were complying in the first place. I edited my post, it seems it was a GCC 2.4 and before problem.

9. A signed size_t variable could be a nightmare when come upon unexpected. lol..... ugly.

10. It's my observation / understanding that having typedefs such as size_t allows programmers to insulate themselves from compilers, operating systems and specific hardware platforms.

For instance, in old DOS program, an integer was 2 bytes. Today it's 4 bytes. (I don't know if this is the actual case, but) size_t could have been defined as 4 bytes for an old DOS program and today, now its still 4 bytes, even though the operating systems and hardware have both advanced. Therefore, having the typedef insulated you, the programmer, from having to change your program to keep up with changes to the operating system and hardware.

Also, it's now common to typedef a char data type, to support wide characters (for Unicode support), again insulating your program (somewhat perhaps) from this type of change as well.

Todd