# Find The Number of Elements in an Array

Page 1 of 1

## 5 Replies - 4751 Views - Last Post: 07 August 2010 - 09:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=184772&amp;s=ec8574b670395d9785779a60ce7bfb66&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 masoug

Reputation: 22
• Posts: 181
• Joined: 30-December 09

# Find The Number of Elements in an Array

Posted 05 August 2010 - 11:52 PM

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
Is This A Good Question/Topic? 1

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

### #2 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## 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 eker676

• Software Engineer

Reputation: 378
• Posts: 1,833
• Joined: 18-April 09

## 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 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

## 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 vector (a C++ array), and use the .size() function.

### #5 baavgai

• Dreaming Coder

Reputation: 6339
• Posts: 13,537
• Joined: 16-October 07

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

Posted 06 August 2010 - 04:20 AM

masoug, on 06 August 2010 - 12:52 AM, said:

I receive the "data[]" via sockets

If that's the case, whatever method used should return some kind of bytes received. Make note of it there.

### #6 masoug

Reputation: 22
• Posts: 181
• Joined: 30-December 09

## 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