🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9486

Re: How to determine the Number of Elements in an Array?

#9486
From: Dennis Jones
Date: Mon, 05 Jul 2004 15:36
19 lines
1625 bytes
"PC Hua" <home-AT-huaonline.DOT.com> wrote in message
news:opsam505lcpmxkmr@newsgroups.borland.com...
> I have something like...
>    std::string SomeStr[]={"a","b","c"};
> I want to run through all elements. How can I get the size of the list
> (number of elements)?

As Andrue has already said, you can do this:

number of elements = sizeof( SomeStr ) / sizeof( SomeStr[0] )

HOWEVER, this will only work for a static array -- that is, an array whose
size is defined at compile-time.  The only way to determine the number of
elements in an array whose size is not static is to use a sentry value as
the last element.  Then you iterate through the array, counting elements as
you go, and stop when you get to the sentry value.

- Dennis
Technical Details
Message-ID: <40e9759b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <opsam505lcpmxkmr@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,befc51f674d02011
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Dennis Jones" <nospam@bogus.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <opsam505lcpmxkmr@newsgroups.borland.com>
Subject: Re: How to determine the Number of Elements in an Array?
Date: Mon, 5 Jul 2004 08:36:58 -0700
Lines: 21
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 204.200.27.7
Message-ID: <40e9759b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1089041819 204.200.27.7 (5 Jul 2004 08:36:59 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail