-4
#include < stdio.h >

void print(int arr[])
{
    int n=sizeof (arr)/sizeof (arr[0]);
    int i;
    for (i=0; i< n; i++)
    {
        printf("%d",arr[i]);
    }
}
int main()
{
    int arr[]={1,2,3,4,5,6};
    print(arr);
}

Q) In this program does sizeof [arr] give the address of base address of array or the size of arr?

1 Answers1

1
sizeof(arr)

will return the size of a pointer to int. What you can do is compute the size of the array outside ot the function and pass it as a function patameter

... 3 Except when it is the operand of the sizeof operator, the _Alignof 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. If the array object has register storage class, the behavior is undefined.

arr will be converted to a pointer to int and you will get the size of that pointer. Also it's important to note that there is a difference between arr and &arr.Arr is of type int* and &arr is of type int (*)[10].

Martin Chekurov
  • 733
  • 4
  • 15