Velocity Reviews > How to find out the size of an array?

# How to find out the size of an array?

Ian Tuomi
Guest
Posts: n/a

 10-10-2003
Say I have an array: int foo[] and it has an unknown number of integers
in it. How can I find out how many? I tried:

#include <stdio.h>

int ArraySize(int array[])
{
int i = 0;
while(array[i] != NULL) i++;
return i;
}

int main(void)
{
int count;
int intarray[10];

for(count = 0;count<=10; ++count)
{
intarray[count] = count;
}

printf("\nintegers in intarray[10] == %d\n", ArraySize(intarray) );

return 0;
}

but it just outputs "integers in intarray[10] == 0". So this must be

--
Ian Tuomi
Jyväskylä, Finland

"Very funny scotty, now beam down my clothes."

GCS d- s+: a--- C++>\$ L+>+++\$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

Mark A. Odell
Guest
Posts: n/a

 10-10-2003
Ian Tuomi <(E-Mail Removed)> wrote in
news:bm6qs8\$s40\$(E-Mail Removed):

> Say I have an array: int foo[] and it has an unknown number of integers
> in it. How can I find out how many? I tried:
>
> #include <stdio.h>
>
> int ArraySize(int array[])

You can't use sizeof on this "array" since it has decayed into a pointer.

> {
> int i = 0;
> while(array[i] != NULL) i++;

Since NULL is zero, and you've put 0 in the first entry of intarray and
since array[0] is zero (NULL is zero) so you never inrement 'i' and return
the initial value of 'i' as zero. Since the sizeof operator is perfect for
determining the size of an array this function is both wrong and
redundant.

> return i;
> }
>
> int main(void)
> {
> int count;
> int intarray[10];
>
> for(count = 0;count < 10; ++count) /* Less than, not <= !!! */
> {
> intarray[count] = count;
> }
>
>
> printf("\nintegers in intarray[10] == %d\n", sizeof intarray);

/* Use sizeof operator */
> return 0;
> }

--
- Mark ->
--

Alex Vinokur
Guest
Posts: n/a

 10-10-2003

"Ian Tuomi" <(E-Mail Removed)> wrote in message news:bm6qs8\$s40\$(E-Mail Removed)...
> Say I have an array: int foo[] and it has an unknown number of integers
> in it. How can I find out how many?

[snip]

n = sizeof(foo)/sizeof(*foo);

==========================
Alex Vinokur
(E-Mail Removed)
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================

Mark A. Odell
Guest
Posts: n/a

 10-10-2003
"Mark A. Odell" <(E-Mail Removed)> wrote in

>> Say I have an array: int foo[] and it has an unknown number of integers
>> in it. How can I find out how many? I tried:

>> printf("\nintegers in intarray[10] == %d\n", sizeof intarray);

> /* Use sizeof operator */

Sorry but I mis-read your question. As Alex responded, the number of
elements in an array is easily calculated by use of sizeof, here's my
macro for doing this.

#define NUM_OF(x) (sizeof (x) / sizeof *(x))

printf("\nintegers in intarray[10] == %d\n", NUM_OF(intarray));

--
- Mark ->
--

Christopher Benson-Manica
Guest
Posts: n/a

 10-10-2003
Ian Tuomi <(E-Mail Removed)> spoke thus:

> #include <stdio.h>

> int ArraySize(int array[])
> {
> int i = 0;
> while(array[i] != NULL) i++;
> return i;
> }

In general, this won't do what you want - if your array happens not to contain
0, your code will blithely continue testing values well beyond your array
against NULL. Your implementation may or may not be kind enough to dump core
on you in such a case.

> for(count = 0;count<=10; ++count)
> {
> intarray[count] = count;
> }

This is bad. The indices of the elements of intarray as you declared it go
from 0 to 9, and this code accesses element 10, which is out of bounds.
Again, your implementation is not required to inform you that anything is
going wrong. Mine doesn't.

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.

Ian Tuomi
Guest
Posts: n/a

 10-10-2003
Mark A. Odell wrote:

>

np.

> here's my macro for doing this.
>
> #define NUM_OF(x) (sizeof (x) / sizeof *(x))

What if you want to know how many "slots" of the array are being used?
for example if I have int foo[10] and only use the first 7 places in the
array it still returns 10.

--
Ian Tuomi
Jyväskylä, Finland

"Very funny scotty, now beam down my clothes."

GCS d- s+: a--- C++>\$ L+>+++\$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

Irrwahn Grausewitz
Guest
Posts: n/a

 10-10-2003
Ian Tuomi <(E-Mail Removed)> wrote:

>Mark A. Odell wrote:

<SNIP>
>> here's my macro for doing this.
>>
>> #define NUM_OF(x) (sizeof (x) / sizeof *(x))

>
>What if you want to know how many "slots" of the array are being used?
>for example if I have int foo[10] and only use the first 7 places in the
>array it still returns 10.

Well, there are /some/ things a programmer has to keep track of
on his own, you know...

--
Irrwahn,
even nowadays not everything is done automagically.

Mark A. Odell
Guest
Posts: n/a

 10-10-2003
Ian Tuomi <(E-Mail Removed)> wrote in
news:bm7568\$2qo\$(E-Mail Removed):

> > here's my macro for doing this.
>>
>> #define NUM_OF(x) (sizeof (x) / sizeof *(x))

>
> What if you want to know how many "slots" of the array are being used?
> for example if I have int foo[10] and only use the first 7 places in the
> array it still returns 10.

You have to come up with some accounting scheme. C has no way of knowing
if a "slot" has been, will be, or is being used.

--
- Mark ->
--

Mark McIntyre
Guest
Posts: n/a

 10-10-2003
On Fri, 10 Oct 2003 20:39:19 +0300, in comp.lang.c , Ian Tuomi
<(E-Mail Removed)> wrote:

>Say I have an array: int foo[] and it has an unknown number of integers
>in it. How can I find out how many?

If you mean "how many are valid data", you can't. The only way to know
how many of the values in the array are valid is to use a guard value,
like C does for strings, where '\0' is used.

If you mean "how long is the array", you also can't. But then you
ought to know that already since you know how large foo was when you
created it. So pass that value in.

>int ArraySize(int array[])

At this point ,despite looking like an array, "array" is actually a
pointer to the first element of the array.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

Mark McIntyre
Guest
Posts: n/a

 10-10-2003
On Fri, 10 Oct 2003 20:03:17 +0200, in comp.lang.c , "Alex Vinokur"
<(E-Mail Removed)> wrote:

>
>"Ian Tuomi" <(E-Mail Removed)> wrote in message news:bm6qs8\$s40\$(E-Mail Removed)...
>> Say I have an array: int foo[] and it has an unknown number of integers
>> in it. How can I find out how many?

>[snip]
>
>n = sizeof(foo)/sizeof(*foo);

Yes, but why bother? Why not just remember how big it was when you
created it ?

double foo[12];
size_t size = sizeof(foo)/sizeof(*foo); // golly, thats twelve too!

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---