Home > Mobile >  How many elements does the compiler take from an array which is function argument?
How many elements does the compiler take from an array which is function argument?

Time:09-13

I have heard that when using an array as a function argument, the compiler takes it as not actually an array but as a pointer to the 1st element of the array. So, when asking for size of A(int array) it should return size of pointer 4 (in bytes). But I am getting 8. What is the problem?

I am using CodeBlocks.

int SumOfElemets(int* A, int size){//       int* A  or  int A[]   ... it is the same!

    int i, sum = 0;
    printf("SOE - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));

//here it should be 4 and 4 but i am getting 8 and 4

    for (i=0;i<size; i  ){

        sum  = A[i];    //A[i] is *(A i)
    }
    return sum;
}


int main(){

    int A[] = {1,2,3,4,5};
    int size = sizeof(A)/sizeof(A[0]);    //size of the array
    int total = SumOfElemets(A, size);
    printf("Sum of elements = %d\n", total);
    printf("Main - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));


    return 0;
}

CodePudding user response:

Hi I have heard that when using an array as a function argument compiler takes it as not actually an array but as a pointer to the 1st element of the array.

Firstly function parameters having array types are adjusted by the compiler to pointers to the array element types.

For example these function declarations

int SumOfElemets(int* A, int size);
int SumOfElemets(int A[], int size);
int SumOfElemets(int A[1], int size);
int SumOfElemets(int A[100], int size);

are equivalent and declare the same one function.

On the other hand, arrays used in expressions as for example argument expressions with rare exceptions are implicitly converted to pointers to their first elements.

So these function calls

SumOfElemets(A, size);
SumOfElemets(&A[0], size);
SumOfElemets(A   0, size);

are equivalent.

The size of pointer is implementation defined as by the way the size of an object of the type int though usually sizeof( int ) or in the case of your code sizeof( A[0] ) is equal to 4.

Pay attention to that expression with the sizeof operator has the type size_t. So you need to use the conversion specifier %zu to output the value of the expression instead of %d

printf("SOE - Size of A = %zu, size of A[0] = %zu\n", sizeof(A), sizeof(A[0])); 

CodePudding user response:

The sizeof operator yields the size (in bytes) of its operand.

When you call sizeof(A), that means "what is the size of the pointer A?".
That is different from sizeof(A[0]), which means "what's the size of the first element of the array?".

The size of types is compiler-dependent, and since in C implementations with a 64-bit address space, pointers are typically 64 bits (eight 8-bit bytes) and int are often 32 bits, therefore:

  • 8 bytes for sizeof(A);
  • 4 bytes for sizeof(A[0]).

(Clarified by Eric Postpischil)


Check out the following examples:
Example with a x64 (64bit address space) compiler: https://godbolt.org/z/vYcWhvxsE
Output:

Size of pointer to int (x64): 8
Size of integer (x64): 4

Example with a x86 (32bit address space) compiler: https://godbolt.org/z/68PTsM6db
Output:

Size of pointer to int (x86): 4
Size of integer (x86): 4
  • Related