5 Replies - 3380 Views - Last Post: 07 August 2010 - 09:48 PM Rate Topic: -----

#1 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • 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) ?
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5820
  • View blog
  • Posts: 12,671
  • Joined: 16-October 07

Re: Find The Number of Elements in an Array

Posted 06 August 2010 - 04:20 AM

View Postmasoug, 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.
Was This Post Helpful? 1
  • +
  • -

#6 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1