By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

How to find out the size of an array?

P: n/a
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
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

Nov 13 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Ian Tuomi <ia*******@co.jyu.fi> wrote in
news:bm**********@phys-news1.kolumbus.fi:
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 ->
--
Nov 13 '05 #2

P: n/a

"Ian Tuomi" <ia*******@co.jyu.fi> wrote in message news:bm**********@phys-news1.kolumbus.fi...
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:al****@connect.to
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================

Nov 13 '05 #3

P: n/a
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn********************************@130.133.1. 4:
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 ->
--
Nov 13 '05 #4

P: n/a
Ian Tuomi <ia*******@co.jyu.fi> 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.
Nov 13 '05 #5

P: n/a
Mark A. Odell wrote:
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

Nov 13 '05 #6

P: n/a
Ian Tuomi <ia*******@co.jyu.fi> 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.
Nov 13 '05 #7

P: n/a
Ian Tuomi <ia*******@co.jyu.fi> wrote in
news:bm**********@phys-news1.kolumbus.fi:
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 ->
--
Nov 13 '05 #8

P: n/a
On Fri, 10 Oct 2003 20:39:19 +0300, in comp.lang.c , Ian Tuomi
<ia*******@co.jyu.fi> 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>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #9

P: n/a
On Fri, 10 Oct 2003 20:03:17 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

"Ian Tuomi" <ia*******@co.jyu.fi> wrote in message news:bm**********@phys-news1.kolumbus.fi...
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>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #10

P: n/a

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:uq********************************@4ax.com...
On Fri, 10 Oct 2003 20:03:17 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

"Ian Tuomi" <ia*******@co.jyu.fi> wrote in message news:bm**********@phys-news1.kolumbus.fi...
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:al****@connect.to
http://mathforum.org/library/view/10978.
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================


Nov 13 '05 #11

P: n/a
Mark McIntyre wrote:
<al****@bigfoot.com> 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #12

P: n/a

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:6g********************************@4ax.com...
On Sat, 11 Oct 2003 10:03:16 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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:al****@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
Nov 13 '05 #13

P: n/a
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #14

P: n/a
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 13 '05 #15

P: n/a
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.com> wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #16

P: n/a
Mark McIntyre <ma**********@spamcop.net> wrote:
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.com> wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 13 '05 #17

P: n/a
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #18

P: n/a

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:fh********************************@4ax.com...
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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:al****@connect.to
http://mathforum.org/library/view/10978.html
=====================================

Nov 13 '05 #19

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.com> wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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.


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
Nov 13 '05 #20

P: n/a
On 12 Oct 2003 04:57:53 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.com> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #21

P: n/a
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #22

P: n/a

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:j8********************************@4ax.com...
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:
[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.
bar (foo1, ITEMS_IN_ARRAY(foo1));


ditto
BAR (foo1);


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


I work on Windows 2000 with GNU gcc compiler.
=====================================
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================

Nov 13 '05 #23

P: n/a
Mark McIntyre wrote:

On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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
:-)


double foo[] = {
#include "coefficients.h"
#if DEBUGGING_ENABLED
0.0, -1.0
#endif
};
/* Could you print here how many elements the foo array contains? */
--
Er*********@sun.com
Nov 13 '05 #24

P: n/a
On Mon, 13 Oct 2003 07:30:29 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:j8********************************@4ax.com...
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:


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

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #25

P: n/a
On Mon, 13 Oct 2003 10:35:55 -0400, in comp.lang.c , Eric Sosman
<Er*********@sun.com> wrote:
Mark McIntyre wrote:

On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> 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
:-)


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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #26

P: n/a

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:3n********************************@4ax.com...
On Mon, 13 Oct 2003 07:30:29 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

"Mark McIntyre" <ma**********@spamcop.net> wrote in message news:j8********************************@4ax.com...
On Sun, 12 Oct 2003 19:59:59 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:
[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:al****@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================

Nov 13 '05 #27

P: n/a
On Tue, 14 Oct 2003 06:59:14 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot.com> wrote:

Whatever. You know what I was trying to say. I expressed it badly,
you're being gratuitously pedantic.
Thats my point. The original posting seemed to me to be saying that
this trick
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
Nov 13 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.