Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

How to find out the size of an array?

 
 
Alex Vinokur
Guest
Posts: n/a
 
      10-11-2003

"Mark McIntyre" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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!
>


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

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




 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      10-11-2003
On Sat, 11 Oct 2003 10:03:16 +0200, in comp.lang.c , "Alex Vinokur"
<(E-Mail Removed)> 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>
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 =---
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-12-2003
Mark McIntyre wrote:
> <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      10-12-2003

"Mark McIntyre" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Sat, 11 Oct 2003 10:03:16 +0200, in comp.lang.c , "Alex Vinokur"
> <(E-Mail Removed)> 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
(E-Mail Removed)
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<(E-Mail Removed)> 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 =---
 
Reply With Quote
 
Floyd Davidson
Guest
Posts: n/a
 
      10-12-2003
Mark McIntyre <(E-Mail Removed)> wrote:
>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
><(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-12-2003
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> wrote:
>>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
>><(E-Mail Removed)> 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 =---
 
Reply With Quote
 
Floyd Davidson
Guest
Posts: n/a
 
      10-12-2003
Mark McIntyre <(E-Mail Removed)> wrote:
>On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
><(E-Mail Removed)> wrote:
>
>>Mark McIntyre <(E-Mail Removed)> wrote:
>>>On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
>>><(E-Mail Removed)> 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) (E-Mail Removed)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-12-2003
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      10-12-2003

"Mark McIntyre" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
> <(E-Mail Removed)> 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
(E-Mail Removed)
http://mathforum.org/library/view/10978.html
=====================================





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments