Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to know the number of elements in an static array.

Reply
Thread Tools

How to know the number of elements in an static array.

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      05-20-2005
Hi,

For example, I have the following array. How can I know how many
elements are in the array?

static int space[] = {140, 150, 160, 170, 180, 190, 200, 210, 220, 235,
250,
270, 290, 320, 355, 440, 500, 570, 655, 755, 855, 1025, 1200};

Best wishes,
Peng

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-20-2005
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>For example, I have the following array. How can I know how many
>elements are in the array?


>static int space[] = {140, 150, 160, 170, 180, 190, 200, 210, 220, 235,
>250,
> 270, 290, 320, 355, 440, 500, 570, 655, 755, 855, 1025, 1200};


sizeof(space) / sizeof(space[0])
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
 
Reply With Quote
 
 
 
 
Mick Sharpe
Guest
Posts: n/a
 
      05-20-2005
Use sizeof(space)/sizeof(int)


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-20-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:
> For example, I have the following array. How can I know how many
> elements are in the array?


> static int space[] = {140, 150, 160, 170, 180, 190, 200, 210, 220, 235,
> 250,
> 270, 290, 320, 355, 440, 500, 570, 655, 755, 855, 1025, 1200};


sizeof space / sizeof *space

or make a macro out of it:

#defined NUM_ELEMS( x ) ( sizeof x / sizeof x[ 0 ] )

and "NUM_ELEMS( start )" will be an integer (of type size_t) with
the number of elements.

Of course, this requires that 'space' is a real array, not just a
pointer to the first element (to what it would be converted to
when you pass 'space' to a function).

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-20-2005
(E-Mail Removed) wrote:
> Hi,
>
> For example, I have the following array. How can I know how many
> elements are in the array?
>
> static int space[] = {140, 150, 160, 170, 180, 190, 200, 210, 220, 235,
> 250,
> 270, 290, 320, 355, 440, 500, 570, 655, 755, 855, 1025, 1200};
>


size_t nelements = sizeof space/sizeof *space;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM



Advertisments