# How to find out the size of an array?

Discussion in 'C Programming' started by Ian Tuomi, Oct 10, 2003.

1. ### Ian TuomiGuest

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 != 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!

Ian Tuomi, Oct 10, 2003

2. ### Mark A. OdellGuest

Ian Tuomi <> wrote in
news:bm6qs8\$s40\$:

> 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 != 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 ->
--

Mark A. Odell, Oct 10, 2003

3. ### Alex VinokurGuest

"Ian Tuomi" <> wrote in message news:bm6qs8\$s40\$...
> 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
mailto:
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================

Alex Vinokur, Oct 10, 2003
4. ### Mark A. OdellGuest

"Mark A. Odell" <> 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 ->
--

Mark A. Odell, Oct 10, 2003
5. ### Christopher Benson-ManicaGuest

Ian Tuomi <> spoke thus:

> #include <stdio.h>

> int ArraySize(int array[])
> {
> int i = 0;
> while(array != 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.

Christopher Benson-Manica, Oct 10, 2003
6. ### Ian TuomiGuest

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!

Ian Tuomi, Oct 10, 2003
7. ### Irrwahn GrausewitzGuest

Ian Tuomi <> 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.

Irrwahn Grausewitz, Oct 10, 2003
8. ### Mark A. OdellGuest

Ian Tuomi <> wrote in
news:bm7568\$2qo\$:

> > 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 A. Odell, Oct 10, 2003
9. ### Mark McIntyreGuest

On Fri, 10 Oct 2003 20:39:19 +0300, in comp.lang.c , Ian Tuomi
<> 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, Oct 10, 2003
10. ### Mark McIntyreGuest

On Fri, 10 Oct 2003 20:03:17 +0200, in comp.lang.c , "Alex Vinokur"
<> wrote:

>
>"Ian Tuomi" <> wrote in message news:bm6qs8\$s40\$...
>> 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 =---

Mark McIntyre, Oct 10, 2003
11. ### Alex VinokurGuest

"Mark McIntyre" <> wrote in message news:...
> On Fri, 10 Oct 2003 20:03:17 +0200, in comp.lang.c , "Alex Vinokur"
> <> wrote:
>
> >
> >"Ian Tuomi" <> wrote in message news:bm6qs8\$s40\$...
> >> 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!
>

double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
Here we have nothing to remember.

--
==========================
Alex Vinokur
mailto:
http://mathforum.org/library/view/10978.
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================

Alex Vinokur, Oct 11, 2003
12. ### Mark McIntyreGuest

On Sat, 11 Oct 2003 10:03:16 +0200, in comp.lang.c , "Alex Vinokur"
<> wrote:

>double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
>Here we have nothing to remember.

Indeed. Anyone can see that its size is five.

I'm not sure I see your point. Do you deny that we know the size of an
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, Oct 11, 2003
13. ### CBFalconerGuest

Mark McIntyre wrote:
> <> wrote:
>
> >double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
> >Here we have nothing to remember.

>
> Indeed. Anyone can see that its size is five.
>
> I'm not sure I see your point. Do you deny that we know the size
> of an array?

and we can easily discover its size by:

sz = (sizeof foo) / (sizeof foo[0];

which conveniently allows us to slave everything to the
declaration and initialization line.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Oct 12, 2003
14. ### Alex VinokurGuest

"Mark McIntyre" <> wrote in message news:...
> On Sat, 11 Oct 2003 10:03:16 +0200, in comp.lang.c , "Alex Vinokur"
> <> wrote:
>
> >double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
> >Here we have nothing to remember.

>
> Indeed. Anyone can see that its size is five.
>
> I'm not sure I see your point. Do you deny that we know the size of an
> array?
>

----------------------------------------------------
#include <stdio.h>
int main()
{
double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
/* Could you print here how many elements the foo array contains? */
return 0;
}
----------------------------------------------------

--
=====================================
Alex Vinokur
mailto:
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================

Alex Vinokur, Oct 12, 2003
15. ### Mark McIntyreGuest

On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<> wrote:

>double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
> /* Could you print here how many elements the foo array contains? */

Yes, because I can count. Evidently elementary numeracy is a dying art

--
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, Oct 12, 2003
16. ### Floyd DavidsonGuest

Mark McIntyre <> wrote:
>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
><> wrote:
>
>>double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
>> /* Could you print here how many elements the foo array contains? */

>
>Yes, because I can count. Evidently elementary numeracy is a dying art
>

How pleasant that you can count to 5. Can you speak his native
language as well has he speaks yours?

I suspect he wanted to know how to obtain and print out the size
of array foo???

#include <stdio.h>
int main(void)
{
double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
printf("array foo contains %u elements\n",
sizeof foo / sizeof (double));
return 0;
}

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>

Floyd Davidson, Oct 12, 2003
17. ### Mark McIntyreGuest

On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<> wrote:

>Mark McIntyre <> wrote:
>>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
>><> wrote:
>>
>>>double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
>>> /* Could you print here how many elements the foo array contains? */

>>
>>Yes, because I can count. Evidently elementary numeracy is a dying art
>>

>
>I suspect he wanted to know how to obtain and print out the size
>of array foo???

Yes, but in general, or in the highly specific case of
T foo[] = {someinitialisers};
?
I'm trying (badly) to point out that C offers no way to reliably
determine the size of an array in nontrivial code, other than
remembering it. The sizeof method is merely a way of remembering it,
IMHO.

--
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, Oct 12, 2003
18. ### Floyd DavidsonGuest

Mark McIntyre <> wrote:
>On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
><> wrote:
>
>>Mark McIntyre <> wrote:
>>>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
>>><> wrote:
>>>
>>>>double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
>>>> /* Could you print here how many elements the foo array contains? */
>>>
>>>Yes, because I can count. Evidently elementary numeracy is a dying art
>>>

>>
>>I suspect he wanted to know how to obtain and print out the size
>>of array foo???

>
>Yes, but in general, or in the highly specific case of
>T foo[] = {someinitialisers};
> ?
>I'm trying (badly) to point out that C offers no way to reliably
>determine the size of an array in nontrivial code, other than
>remembering it. The sizeof method is merely a way of remembering it,
>IMHO.

Gee, and here I thought you were just being an asshole.

I still do.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>

Floyd Davidson, Oct 12, 2003
19. ### CBFalconerGuest

Alex Vinokur wrote:
>

.... snip ...
....
> #include <stdio.h>
> int main()

int main(void)
> {
> double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
> /* Could you print here how many elements the foo array contains? */

printf("%d\n", (int)(sizeof foo / sizeof foo[0]));
> return 0;
> }

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Oct 12, 2003
20. ### Alex VinokurGuest

"Mark McIntyre" <> wrote in message news:...
> On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
> <> wrote:
>
> >double foo[] = {1.0, 2.0, 3.0, 4.0, 5.0};
> > /* Could you print here how many elements the foo array contains? */

>
> Yes, because I can count. Evidently elementary numeracy is a dying art
>
>

--------- C code ---------
#include <stdio.h>

#define ITEMS_IN_ARRAY(x) sizeof(x)/sizeof(*(x))
#define BAR(x) bar(x, sizeof(x)/sizeof(*(x)))

void bar (double* array, int no_of_items_in_array)
{
printf ("bar : %d\n", no_of_items_in_array);
// Stuff
}

void bar2 (double* array, int no_of_items_in_array)
{
printf ("bar2 : %d\n", no_of_items_in_array);
// Stuff
}

int main()
{
double foo1[0];
double foo2[] = {};
double foo3[5];
double foo4[] = {1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0};

// --- Method-1
bar (foo1, 0);
bar (foo2, 0);
bar (foo3, 5);
bar (foo4, 7);
printf ("\n");

bar2 (foo1, 0);
bar2 (foo2, 0);
bar2 (foo3, 5);
bar2 (foo4, 7);
printf ("\n");

printf ("\n");

// --- Method-2
bar (foo1, sizeof(foo1)/sizeof(*foo1));
bar (foo2, sizeof(foo1)/sizeof(*foo2));
bar (foo3, sizeof(foo1)/sizeof(*foo3));
bar (foo4, sizeof(foo1)/sizeof(*foo4));
printf ("\n");

bar2 (foo1, sizeof(foo1)/sizeof(*foo1));
bar2 (foo2, sizeof(foo1)/sizeof(*foo2));
bar2 (foo3, sizeof(foo1)/sizeof(*foo3));
bar2 (foo4, sizeof(foo1)/sizeof(*foo4));
printf ("\n");

printf ("\n");

// --- Method-3
bar (foo1, ITEMS_IN_ARRAY(foo1));
bar (foo2, ITEMS_IN_ARRAY(foo2));
bar (foo3, ITEMS_IN_ARRAY(foo3));
bar (foo4, ITEMS_IN_ARRAY(foo4));
printf ("\n");

printf ("\n");

// --- Method-4
BAR (foo1);
BAR (foo2);
BAR (foo3);
BAR (foo4);
printf ("\n");

printf ("\n");

// ------
return 0;
}
--------------------------

If it turns out that you need to change foo1, ..., foo4,
what changes must be done in the program above?

I think the Method-1 is not handy in this context.

=====================================
Alex Vinokur
mailto:
http://mathforum.org/library/view/10978.html
=====================================

Alex Vinokur, Oct 12, 2003