How to find out the size of an array?

I

Ian Tuomi

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
very wrong. thanks in advance.

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

NOTE: Remove NOSPAM from address
 
M

Mark A. Odell

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 */
 
A

Alex Vinokur

Ian Tuomi said:
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:[email protected]
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================
 
M

Mark A. Odell

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));
 
C

Christopher Benson-Manica

Ian Tuomi said:
#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.
 
I

Ian Tuomi

Mark said:
Sorry but I mis-read your question.
> 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!

NOTE: Remove NOSPAM from address
 
I

Irrwahn Grausewitz

Ian Tuomi said:
Mark A. Odell wrote:
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... ;-)
 
M

Mark A. Odell

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.
 
M

Mark McIntyre

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.
 
M

Mark McIntyre

Ian Tuomi said:
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!
 
A

Alex Vinokur

Mark McIntyre said:
Ian Tuomi said:
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:[email protected]
http://mathforum.org/library/view/10978.
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================
 
M

Mark McIntyre

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?
 
C

CBFalconer

Mark said:
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.
 
A

Alex Vinokur

Mark McIntyre said:
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:[email protected]
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
 
M

Mark McIntyre

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
:)
 
F

Floyd Davidson

Mark McIntyre said:
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;
}
 
M

Mark McIntyre

Mark McIntyre said:
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.
 
F

Floyd Davidson

Mark McIntyre said:
Mark McIntyre said:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"

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.
 
C

CBFalconer

.... 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]));
 
A

Alex Vinokur

Mark McIntyre said:
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:[email protected]
http://mathforum.org/library/view/10978.html
=====================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top