468,772 Members | 1,848 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

Problem with calculating the size of an array

Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:

int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));
}
main() {
int a[3]={1,2,3};
printf("%d\n",SizeOfArray(a));
}

but is right when the array declared in the same scope as the sizeof()
statment,

main() {
int a[3]={1,2,3};
printf("%d\n",sizeof(a)/sizeof(a[0]));
}

why's that ? i suspect that i passed the wrong type of parameter to
the function. (should be &a or *a or something... ??)
Nov 14 '05 #1
5 11664
ru****@sohu.com (sugaray) wrote in
news:ad**************************@posting.google.c om:
Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:

int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));


Bang! You have tricked yourself. Although you think you are passing an
array into SizeOfArray() you are actually passing a pointer which on
32-bit platforms is often only 4 bytes in size. You can (1) pass in the
size, (2)package the array inside a struct, or (3) simply USE THE C sizeof
OPERATOR!
#include <stdio.h>

struct ArrayWrap
{
int array[100];
} pkg;

int array[100];

void printPkgArrSize(struct ArrayWrap *pPkg)
{
printf("Size of array is %u bytes\n", sizeof pPkg->array);
}

void printArrSize(size_t size)
{
printf("Size of array is %u bytes\n", size);
}

int main(void)
{
/* 1
*/
printPkgArrSize(&pkg);

/* 2
*/
printArrSize(sizeof array);

/* 3
*/
printf("Size of array is %u bytes\n", sizeof array);

return 0;
}
--
- Mark ->
--
Nov 14 '05 #2

"sugaray" <ru****@sohu.com> wrote in message
news:ad**************************@posting.google.c om...
Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:

int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));
}
main() {
int a[3]={1,2,3};
printf("%d\n",SizeOfArray(a));
}
[..]
the function. (should be &a or *a or something... ??)


Actually, SizeOfArray () returns the number of elements
of the array. The above function would not work.
I would write the function as below, though, I would
prefer a macro to function:

#include <stdio.h>
#include <stdlib.h>

#define MAX 10

size_t
len_of_array ( int (*a)[MAX] )
{
printf ( "Array size: %lu\n", sizeof ( *a ) );
return sizeof *a / sizeof **a;
}

int
main ( void )
{
int a[MAX];
printf ( "Number of elements: %lu\n", len_of_array ( &a ) );
return EXIT_SUCCESS;
}
--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #3
nrk
Vijay Kumar R Zanvar wrote:
#include <stdio.h>
#include <stdlib.h>

#define MAX 10

size_t
len_of_array ( int (*a)[MAX] )
{
printf ( "Array size: %lu\n", sizeof ( *a ) );
return sizeof *a / sizeof **a;
}

This is equivalent to:
size_t len_of_array(void) { return MAX; }

It might be useful to define a macro:

#define NELEMS(x) (sizeof x/sizeof x[0])

and use that instead.

-nrk.
int
main ( void )
{
int a[MAX];
printf ( "Number of elements: %lu\n", len_of_array ( &a ) );
return EXIT_SUCCESS;
}


--
Remove devnull for email
Nov 14 '05 #4
"Mark A. Odell" <no****@embeddedfw.com> wrote in message news:<Xn********************************@130.133.1 .4>...
ru****@sohu.com (sugaray) wrote in
news:ad**************************@posting.google.c om:
Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:

int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));


Bang! You have tricked yourself. Although you think you are passing an
array into SizeOfArray() you are actually passing a pointer which on
32-bit platforms is often only 4 bytes in size. You can (1) pass in the
size, (2)package the array inside a struct, or (3) simply USE THE C sizeof
OPERATOR!


or 4) implement the functionality as a macro. Use of function
macros is always to be done with some hesitation, but this
one seems OK:

#define N_ELTS(a) ((int)(sizeof(a)/sizeof(a[0])))

I put in the cast to emulate his function semantics, as he wants
to use the value as an int, not a size_t. Of course, this macro
only works if the array definition is in scope, otherwise
one of the solutions Mark proposed seems needed. In real life,
packaging the array and its size into a struct keeps all the info
in one place and that is nice... Hmmm, not having a C99 standard
at hand, what happens if this macro is applied to a VLA?
sizeof evaluated at runtime? error?

-David
Nov 14 '05 #5
Groovy hepcat sugaray was jivin' on 22 Jan 2004 05:58:12 -0800 in
comp.lang.c.
Problem with calculating the size of an array's a cool scene! Dig it!
Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:

int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));


Why didn't you pay attention when you read the FAQ? I know you did
read it, because it is a requirement before posting to the newsgroup.
Noone wants to be thought of as an idiot, so you must have read the
FAQ. However, you obviously missed question 6.21, because you surely
would have asked for clarification had you read it and misunderstood.
So, the conclusion I'm drawing is that you just weren't paying
attention at all. You simply couldn't be bothered. Well, why should we
bother to help you, then?
If you want our help, you're going to have to prove it. You're going
to have to show us that you are looking for answers and are willing to
learn. Start by going to the FAQ's web site,
http://www.eskimo.com/~scs/C-faq/top.html, and reading it through
Then, read at least a month worth of articles in this newsgroup. Make
sure you read the article entitled "Welcome to comp.lang.c!" by James
Hu.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by PHil Coveney | last post: by
15 posts views Thread by Peter Afonin | last post: by
22 posts views Thread by Wilson | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.