Problem with calculating the size of an array

S

sugaray

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... ??)
 
M

Mark A. Odell

(e-mail address removed) (sugaray) wrote in

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;
}
 
V

Vijay Kumar R Zanvar

sugaray said:
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;
}
 
N

nrk

Vijay said:
#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;
}
 
D

David Resnick

Mark A. Odell said:
(e-mail address removed) (sugaray) wrote in

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
 
P

Peter Shaggy Haywood

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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,345
Latest member
tektheone

Latest Threads

Top