Hi,

If I have just an array, such as "data[]", is there a way to find the number of elements in this array?

In my program, I receive the "data[]" via sockets, but I don't know the number of elements in the array, so is there a function that can do this? All I have is just "data[]".

Thanks!!!

-Masoug

# Find The Number of Elements in an Array

Page 1 of 1## 5 Replies - 5564 Views - Last Post: 07 August 2010 - 09:48 PM

##
**Replies To:** Find The Number of Elements in an Array

### #2

## Re: Find The Number of Elements in an Array

Posted 06 August 2010 - 12:23 AM

What about the classic way of calculating the length of array? sizeof(data) / sizeof (type of data) ?

### #3

## Re: Find The Number of Elements in an Array

Posted 06 August 2010 - 12:28 AM

sizeof(data) / sizeof (type of data)

If the data is being passed as a pointer which most arrays are then your going to have to find a different way.

What about receiving a number and then the array? The number being the amount of elements.

### #4

## Re: Find The Number of Elements in an Array

Posted 06 August 2010 - 02:31 AM

Are you using C or C++?

in C, you would normally either use a sentinel object/value at the end of the data in your array, or a size_t variable to keep track of the number of stored elements.

in C++, you would use a

in C, you would normally either use a sentinel object/value at the end of the data in your array, or a size_t variable to keep track of the number of stored elements.

in C++, you would use a

*vector*(a C++ array), and use the .size() function.### #5

## Re: Find The Number of Elements in an Array

Posted 06 August 2010 - 04:20 AM

### #6

## Re: Find The Number of Elements in an Array

Posted 07 August 2010 - 09:48 PM

Thanks a lot guys!

I just defined a constant number that all the programs would use.

-Masoug

I just defined a constant number that all the programs would use.

-Masoug

Page 1 of 1