Vignesh K Viki
Vignesh K Viki

Reputation: 31

how sizeof() works in pass by reference arguments

I passed a array to function and tried to find the length of the array . but the result was not expected . can anybody explain please?

int main()
{
     int array[10]={0};
     func(array);
     return 0;
}
void func(int arr[])
{
    printf("length of an array is %d ",(sizeof(arr)/sizeof(arr[0]));
}

it gives the answer 2. when I tried the same operation inside main function it works fine (answer is 10). //using gcc complier in linux

Upvotes: 2

Views: 515

Answers (6)

Arjun Sreedharan
Arjun Sreedharan

Reputation: 11453

When you pass an array into a function as a parameter, the array decays into a pointer to the beginning of the array. A pointer has no meta information stored in it unlike the array. A good practice is to pass the size to the function as an additional parameter.

void func(int *arr, int size);

Here's what C standard says (C99 6.3.2.1/3 - Other operands - Lvalues, arrays, and function designators):

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue

So, once you pass it to the function - Inside the function, it's no more an array, it's a pointer.

Upvotes: 0

Paulo Bu
Paulo Bu

Reputation: 29804

Seems to me that the result is caused because sizeof(arr) == 8 (size of a pointer on your PC) and sizeof(arr[0]) == 4 because it is an integer hence 8/4==2.

This declaration: void func(int arr[]) tells the function to expect a pointer to int as argument.

It is not clear to me whether is possible to calculate the sizeof an array by reference. C functions accepting array reference as arguments always tend to receive their length too as argument.

The difference with main() function is that inside main array variable is of type int[10], thus sizeof is able to get its length in bytes. In func, arr is of type int* so sizeof gives you only the length in bytes of a pointer.

Upvotes: 3

gangadhars
gangadhars

Reputation: 2728

You are sending and recieving a array pionter, not the array.

while sending arg::

func(array[10]);

While Receiving arg:

void func(int array[10])

But it's not good to send the total array. So send and receive arguments like below.

func(array, arry_size);  //function call
void func(int array[], int length)  //defin

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409374

You have to remember that arrays decays to pointers. That means that in the function func the variable arr is not actually an array but a pointer. And doing sizeof on a pointers returns the size of the pointer and not what it points to.

You get the result 2 because you're on a 64-bit system where pointers are 64 bits (i.e. 8 bytes), an when divided by the size of int (which is 4 bytes) you get the result 2.

Upvotes: 1

brokenfoot
brokenfoot

Reputation: 11649

sizeof(arr)

is the size of a pointer, not the size of the block arr points to.

Upvotes: 0

Shriram
Shriram

Reputation: 4411

sizeof() operator returns the size of the data type. here int size is two.

Upvotes: -1

Related Questions