How to find out the size of an array?

M

Micah Cowan

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.

The difference is, its the C implementation's responsibility to
remember it in this case, not yours. And regardless of whether
it's "merely a way fo remembering it" or not, it will clearly
give you the number of elements in foo for

T foo[] = {someinitialisers};

So what's the problem?

-Micah
 
M

Mark McIntyre

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

You're entitled to your opinion. You're wrong, but entitled to it
anyway. I was trying to point out... oh, whats the point? You're too
hung up on the cleverness of the solution to care.
I still do.

Like I care because? To use an americanism.
 
M

Mark McIntyre

On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"

interesting examples, mostly snipped.
#define BAR(x) bar(x, sizeof(x)/sizeof(*(x)))

bar (foo3, 5);

Works, provided param 2 is the right number.
bar (foo1, sizeof(foo1)/sizeof(*foo1));

confusingly (to many) fails if foo1 was an argument to the function
calling bar, is an alias for an array, or was a pointer in the first
place.
bar (foo1, ITEMS_IN_ARRAY(foo1));
ditto

BAR (foo1);

this is truly gastly. Do you work for MS? :)

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

struct foothing
{
double somearray[12];
int sizeofarray[12];
}

now pass foothing to each fn, and you have to change 2 lines of code,
one of which you'd have had to change anyway. If you really want to,
put the sizeof() thing in there. Presto, we're both happy.
 
A

Alex Vinokur

Mark McIntyre said:
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
[snip]
bar (foo1, sizeof(foo1)/sizeof(*foo1));

confusingly (to many) fails if foo1 was an argument to the function
calling bar, is an alias for an array, or was a pointer in the first
place.

Here foo1 must be an array name.
this is truly gastly. Do you work for MS? :)

I work on Windows 2000 with GNU gcc compiler.


=====================================
Alex Vinokur
mailto:[email protected]
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
 
E

Eric Sosman

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

double foo[] = {
#include "coefficients.h"
#if DEBUGGING_ENABLED
0.0, -1.0
#endif
};
/* Could you print here how many elements the foo array contains? */
 
M

Mark McIntyre

Mark McIntyre said:
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
[snip]
bar (foo1, sizeof(foo1)/sizeof(*foo1));

confusingly (to many) fails if foo1 was an argument to the function
calling bar, is an alias for an array, or was a pointer in the first
place.

Here foo1 must be an array name.

Thats my point. The original posting seemed to me to be saying that
this trick *always* worked for an array, in any part of your code. No
mention was made of what happens when you pass an array into a
function. Its a common newby mistake not to realise that arrays decay
into pointers in such circumstances, or to think that malloced
"arrays" are also arrays.
 
M

Mark McIntyre

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

double foo[] = {
#include "coefficients.h"
#if DEBUGGING_ENABLED
0.0, -1.0
#endif
};
/* Could you print here how many elements the foo array contains? */

And could you, once you've passed foo to a function?

There's always a pathological case to argue for any tricksy construct.
My point remains, you, the programmer, have to remember how big your
arrays are. Whether you do this at write or compile time is
irrelevant, you still have to remember it.
 
A

Alex Vinokur

Mark McIntyre said:
Mark McIntyre said:
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
[snip]

bar (foo1, sizeof(foo1)/sizeof(*foo1));

confusingly (to many) fails if foo1 was an argument to the function
calling bar, is an alias for an array, or was a pointer in the first
place.

Here foo1 must be an array name.

Thats my point. The original posting seemed to me to be saying that
this trick
It is not a trick. It is a feature.
*always* worked for an array, in any part of your code. No
mention was made of what happens when you pass an array into a
function. Its a common newby mistake not to realise that arrays decay
into pointers in such circumstances, or to think that malloced
"arrays" are also arrays.

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

void bar1 (double* array, int no_of_items_in_array)
{
printf ("\nitems in array : \%d\n", no_of_items_in_array);
printf ("sizeof(array) : %d\n", sizeof (array));
}

void bar2 (double array[], int no_of_items_in_array)
{
printf ("\nitems in array : \%d\n", no_of_items_in_array);
printf ("sizeof(array) : %d\n", sizeof (array));
}

void bar3 (double array[10], int no_of_items_in_array)
{
printf ("\nitems in array : \%d\n", no_of_items_in_array);
printf ("sizeof(array) : %d\n", sizeof (array));
}

int main()
{
double foo[10];

bar1 (foo, sizeof(foo)/sizeof(*foo));
bar2 (foo, sizeof(foo)/sizeof(*foo));
bar3 (foo, sizeof(foo)/sizeof(*foo));
return 0;
}

--------------------------

--------- Output ---------

items in array : 10
sizeof(array) : 4

items in array : 10
sizeof(array) : 4

items in array : 10
sizeof(array) : 4

--------------------------


=====================================
Alex Vinokur
mailto:[email protected]
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
 
M

Mark McIntyre

On Tue, 14 Oct 2003 06:59:14 +0200, in comp.lang.c , "Alex Vinokur"

Whatever. You know what I was trying to say. I expressed it badly,
you're being gratuitously pedantic.
It is not a trick. It is a feature.

Is english your first language? If not, you might like to know that
"trick" is used in english to describe something clever as well as
something naughty.
void bar1 (double* array, int no_of_items_in_array)

Like I said, you need to remember the size of the array.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top