4

I was finding the output of the following C program, which I found on GeeksforGeeks. Here's the program:

#include <stdio.h>
void fun(int ptr[])
{
    int i;
    unsigned int n = sizeof(ptr)/sizeof(ptr[0]);
    for (i=0; i<n; i++)
    printf("%d  ", ptr[i]);
}

// Driver program
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    fun(arr);
    return 0;
}

The output of this code was "1 2". But according to me, the output should be just 1. Here is my interpretation of this code:

  1. Firstly, the main function will run, in which after declaring the array "arr", next statement will execute which contains the statement fun(arr).
  2. In that statement, the function "fun" will be called with the argument arr, which contains the address of the first element of the array.
  3. After that, under the function fun, there is a pointer ptr as a parameter. When this function will execute, then the value of n will be calculated as 1 since here the size of ptr is 4 and the size of ptr[0] is also 4.
  4. Next, the loop will run only once since the value of n is 1 and that's why only '1' will get printed since it is the value of ptr[0].

Please help me to find out where I am wrong.

SiggiSv
  • 1,219
  • 1
  • 10
  • 20
KhiladiBhaiyya
  • 633
  • 1
  • 14
  • 43
  • 2
    note that in C you cannot find the size of your `ptr` (ie number of elements) in `fun` just by inspections. The sizeof calculation you use only works if ptr is a fixed size array (like `int ptr[5]`) – pm100 May 16 '17 at 18:00
  • 1
    @pm100 that is no guarantee: you can pass an array of any length to that. – Weather Vane May 16 '17 at 18:02
  • @WeatherVane, the sizeof calculation will always return 1 or 2 (depending on pointer size). You cannot find the size of int[] it int * arrays by inspection in C. You have to passthe length into the function too (or have a sentinel value) – pm100 May 16 '17 at 18:04
  • @pm100 I know that (which is why I originally duped the question) and so does OP. But `int ptr[5]` still tells you nothing about the length of the array passed, unless there is a `typedef`. – Weather Vane May 16 '17 at 18:06
  • @dg4 A more relevant title is needed - something about `sizeof` – chux - Reinstate Monica May 16 '17 at 18:14

2 Answers2

10
  • [....] the value of n will be calculated as 1 since here the size of ptr is 4 and the size of ptr[0] is also 4.

Well, that's common, but not guaranteed.

sizeof(ptr) could very well be, result in 8, which is likely in your case, while sizeof(int) can evaluate to 4, resulting a value of 2 for n. This depends on (and varies with) your environment and used implementation.

Try printing them separately, like

  • printf("Pointer size :%zu\n", sizeof(ptr));
  • printf("Element size: %zu\n", sizeof(ptr[0]));

and see for yourself.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
7

The size of a pointer on modern platforms is commonly either 4 or 8 bytes.

On a 32-bit platform it's likely that sizeof(ptr) == 4 and n == 1.
On a 64-bit platform it's likely that sizeof(ptr) == 8 and n == 2.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578