0

Look at the code below.

    #include<stdio.h>
    void fun(int *arr)
    {
        int i;
        unsigned int n = sizeof(arr)/sizeof(arr[0]);
    }

   // Driver program
   int main()
   {
        int arr[] = {10, 20, 3, 4, 5, 6, 7, 8};
        printf("%d\n",sizeof(arr));
        fun(arr);
        return 0;
   }

Output of this code is: 32 1

And I belive that's because *arr is pointing only to the base element of the array.

But,

    #include <stdio.h>
    void fun(char *arr)
    {
        int i;
        unsigned int n = sizeof(arr);
        printf("n = %d\n", n);
    }

    // Driver program
    int main()
    {
        char arr[] = "abc";
        fun(arr);
        return 0;
    }

The output of this code is 8. Why so?

Another doubt of mine is array itself is implemented with pointers.So even in main() of the 1st program, the variable arr is a pointer (*arr will give 10). So how come 1st sizeof prints 32?

Mithra
  • 21
  • 5

0 Answers0