473,889 Members | 1,327 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 139012
On 12 Oct 2003 04:57:53 -0800, in comp.lang.c , Floyd Davidson
<fl***@barrow.c om> 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.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 #21
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.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 #22

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

Nov 13 '05 #23
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_ENABL ED
0.0, -1.0
#endif
};
/* Could you print here how many elements the foo array contains? */
--
Er*********@sun .com
Nov 13 '05 #24
On Mon, 13 Oct 2003 07:30:29 +0200, in comp.lang.c , "Alex Vinokur"
<al****@bigfoot .com> wrote:

"Mark McIntyre" <ma**********@s pamcop.net> wrote in message news:j8******** *************** *********@4ax.c om...
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.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 #25
On Mon, 13 Oct 2003 10:35:55 -0400, in comp.lang.c , Eric Sosman
<Er*********@su n.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_ENABL ED
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.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 #26

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

"Mark McIntyre" <ma**********@s pamcop.net> wrote in message news:j8******** *************** *********@4ax.c om...
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(arr ay) : %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(arr ay) : %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(arr ay) : %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****@c onnect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=============== =============== =======

Nov 13 '05 #27
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.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 #28

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
4958
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
2771
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
15945
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
12188
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
9807
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11188
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...
0
10789
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10889
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,...
1
7993
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
5828
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6028
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3255
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.