-1

Can anyone explain me the output of the below program?

   #define SIZE 10
  void size(int arr[SIZE],int i[SIZE])
  {
         printf("size of array is:%d %d\n",sizeof(arr),sizeof(i));
  }

  int main()
    {
        int arr[SIZE],i[SIZE];
       printf("%d %d\n",sizeof(arr),sizeof(i));
        size(arr,i);
        return 0;
     }                                                                                

      OUTPUT
      40 40 
     8 8

why 8 8? plz help

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
CodeHacker
  • 31
  • 6

3 Answers3

2

Because the arguments int arr[SIZE] and int i[SIZE] are not arrays like they were in the main function. They are pointers. And pointers generally have size 8 on 64-bit machines.

randomusername
  • 7,927
  • 23
  • 50
  • @CodeHacker If it was 8 the first time that it ran on your machine, then it will continue to alway be 8. But on another computer, it might be different. – randomusername Sep 02 '14 at 16:49
2
  1. In main() you are getting size of array.

  2. When we call size(arr, i);, we are sending the base address of arr and i. When we pass address of a variable to a function, it will store the address in a pointer. So you are getting the size of pointer in your size() function.

ani627
  • 5,578
  • 8
  • 39
  • 45
  • @TheParamagneticCroissant: Yes you are right they are not same! I have made an edit. Thanks for pointing it out :) `arr` gives the address of first int, whereas `&arr` gives the address of array of _ints_. Also `arr+1` will give the address of the next integer, whereas `&arr+1` will give the address of next array of `10` integers. – ani627 Sep 03 '14 at 05:34
-2

Arrays themselves substituted to pointers (but they are not pointer, however). So, code

a[i]

is equivalent to

*(a + i)

When array is not dynamic, sizeof operator can determine it's size (within it's scope), but when it is passed to function it is a pointer. So

sizeof(any_pointer) == sizeof(size_t)

due to result, seems you work on 64-bit processor

Ivan Ivanov
  • 2,076
  • 16
  • 33
  • 1
    `sizeof(any_pointer) == sizeof(size_t)` - do you mean to say that this is always true? because it isn't... – The Paramagnetic Croissant Sep 02 '14 at 16:50
  • Not any pointer, but without any other circumstances... Can you make example where pointer is not tiny, huge, etc. and it is not size_t; I am really interested. – Ivan Ivanov Sep 02 '14 at 16:52
  • then, how is it relevant to the question? OP is not asking anything about the size of `size_t`. – The Paramagnetic Croissant Sep 02 '14 at 16:54
  • He asked about 8 bytes. Why 10*sizeof(int) == 40 and pointer is 8 – Ivan Ivanov Sep 02 '14 at 16:54
  • Yes, he did. There's therefore no need to bring `sizeof(size_t)` into the picture -- it will only confuse OP. – The Paramagnetic Croissant Sep 02 '14 at 16:55
  • Why it will confuse? And when pointer is not size_t? – Ivan Ivanov Sep 02 '14 at 16:55
  • when i give array size as array[10]. it assigns array from 0 to 10 So why it is not 44 bytes?? – CodeHacker Sep 02 '14 at 16:59
  • @CodeHacker first, that's 0 to 9, so at most 40 would be acceptable. And it's not an array but a pointer *because the C specification says so.* – The Paramagnetic Croissant Sep 02 '14 at 17:00
  • arr points to the beginning of array. sizeof(arr) returns within scope it's size (number of elements multiply sizeof(element)). When you call function address of first element of array is passed. – Ivan Ivanov Sep 02 '14 at 17:04
  • @IvanIvanov I used a system once where `size_t` was 16 bits and `void *` was 17 bits – M.M Sep 03 '14 at 05:56
  • size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof operator. The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits and on a 64-bit one - 64 bits. In other words, a pointer can be safely put inside size_t type (an exception is class-function-pointers but this is a special case). That is only thing I believe in, I mean I am wrong, 17 bit is nice and so on, but for me that is frustrating. – Ivan Ivanov Sep 03 '14 at 06:18