473,883 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find out the size of an array?

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("\ninteg ers in intarray[10] == %d\n", ArraySize(intar ray) );

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
27 139011

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

"Ian Tuomi" <ia*******@co.j yu.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****@c onnect.to
http://mathforum.org/library/view/10978.
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=============== ===========


Nov 13 '05 #11
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #12

"Mark McIntyre" <ma**********@s pamcop.net> wrote in message news:6g******** *************** *********@4ax.c om...
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****@c onnect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=============== =============== =======
Nov 13 '05 #13
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.c om/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
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoo t.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.co m
Nov 13 '05 #15
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.c om> wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfo ot.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[] = {someinitialise rs};
?
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.c om/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
Mark McIntyre <ma**********@s pamcop.net> wrote:
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow. com> wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigf oot.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[] = {someinitialise rs};
?
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.co m
Nov 13 '05 #17
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #18

"Mark McIntyre" <ma**********@s pamcop.net> wrote in message news:fh******** *************** *********@4ax.c om...
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****@c onnect.to
http://mathforum.org/library/view/10978.html
=============== =============== =======

Nov 13 '05 #19
Mark McIntyre <ma**********@s pamcop.net> writes:
On 12 Oct 2003 02:45:29 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.c om> wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 12 Oct 2003 06:55:35 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfo ot.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[] = {someinitialise rs};
?
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[] = {someinitialise rs};

So what's the problem?

-Micah
Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
571
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
2
10328
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
3
7261
by: Manish Jain | last post by:
Platform: C# (Windows Application)/XP/Framework 1.1 ----------------------------------------------------------- Hi I am doing some performance checks, I am using a hashtable that I suspect is not implemented effeciently. Can someone tell me how I can find the size of memory occupied by the hashTable?
2
4102
by: Lee Walters | last post by:
Hi All I'm doing a 'select * from table' in my DataReader below and populating an array with the results. Obvisouly I'm only getting the first field from the results by the use of the GetString(0). My Question is how do I find out how many elements there are to GetString without having to know how many fields are in the table? Thanks
43
4956
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy. That is, replace calls to strcpy with say, my_strcpy(dest,src) which will internally find the destination buffer length. For this we need to know the destination buffer size. For statically allocated strings sizeof is returning the length of the...
3
9166
by: Henrootje | last post by:
I have a database in which regurlarily tables are being created or deleted. This causes a lot of bloating. I set the property that causes the db to compress and repair at close but my users started complaining. Loudly. now I thought it would be a good idea if I could build a check along the lines: if database.size 25 mb then compress
1
2770
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define the fixed size using In the attached console application example when I try to address the fields in the class I get an execption.
2
15943
by: Chetanhl | last post by:
Hey guyz if i wanna know how to find size of unsigned char*? void func1(unsigned char* str) { int a; a=?? ////////////////now how to store size of str in a???? }
7
12186
by: Tapas Bose | last post by:
Hello. I want to find size of a dynamically allocated array in my following code : #include <iostream> #include <cstdlib> using namespace std; int main() {
1
3010
by: peachdot | last post by:
hi, how do i create an unlimited array size?
0
9935
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10844
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10410
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7970
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7122
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.