2

I was reading more about arrays vs pointers in C and wrote the following program.

#include <stdio.h> 

int arr[10] = { } ; 
typedef int (*type)[10]  ;
int main()
{
   type val = &arr ; 
   printf("Size is %lu\n", sizeof(val)) ; 
   printf("Size of int is %lu\n", sizeof(int)) ;
}

If, I execute this program, then sizeof(val) is given to be 8 and sizeof(int) is given to be 4.

If val is a pointer to the array with 10 elements, shouldn't it's size be 40. Why is the sizeof(val) 8 ?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97

6 Answers6

3

If val is a pointer to the array...

Yes, it is, and sizeof(val) produces the size for the "pointer to the array", not the array itself.

...shouldn't it's size be 40.?

No, sizeof(val) calculates the size of the operand, the "pointer" here. In your platform, the size of a pointer seems to be 64 bits, i.e., 8 bytes. So, it gives 8.

Also, as I mentioned, use %zu to print size_t, the type produced by sizeof operator.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • If that is the case, then what is the difference b/w the types int (*) [10] and types int* ? . How are they different ? – Pratik Singhal Jun 25 '15 at 08:17
  • @ps06756 Maybe [this](http://stackoverflow.com/a/30892273/2173917) answer can help to clear your doubts? – Sourav Ghosh Jun 25 '15 at 08:20
  • 1
    @ps06756 `int(*)[10]` means a pointer to an array of 10 `int`s while `int*` is just a pointer to an `int`. – Spikatrix Jun 25 '15 at 08:25
  • @CoolGuy...(_just to complete your sentence_)..but, _still_, both **are** pointers. – Sourav Ghosh Jun 25 '15 at 08:26
  • @CoolGuy Exactly, what I meant was, how is pointing to a whole array different from pointing to its first element ? – Pratik Singhal Jun 25 '15 at 08:28
  • 1
    @ps06756 you wrote `sizeof(int)` in your question, that is not a pointer. Did you mean `sizeof(int *)`?? – Sourav Ghosh Jun 25 '15 at 08:29
  • No, that wasn't a typo. I was just checking the sizeof int on my machine – Pratik Singhal Jun 25 '15 at 08:30
  • @ps06756 "how is pointing to a whole array different from pointing to its first element ?" -- The pointers are indeed of different types but since both are effectively a *pointer*, `sizeof` returns the size of an `int*` pointer and not the array. – Spikatrix Jun 25 '15 at 08:32
3

First of all this initialization of an array

int arr[10] = { } ; 

is invalid in C. You may not use emplty braces in C (in C++ they are allowed). You have to write

int arr[10] = { 0 } ; 

In C the corresponding initializer is defined the following way

initializer:
    { initializer-list }
    { initializer-list , }

while in C++

braced-init-list:
    { initializer-list ,opt }
    { }

As for this statement

printf("Size is %lu\n", sizeof(val)) ; 

then val is a pointer because it has type type defined like

typedef int (*type)[10]  ;

Change this statement to

printf( "Size is %zu\n",  sizeof( *val ) ); 

if you want to get the size of the object (that is of the array) pointed to by the pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Good Morning Sir!! Regarding your first statement, can you confirm that empty initializer is invalid in `c99` also? I am sure ISO C forbids it, but not very sure about c99, can you confirm? – Sourav Ghosh Jun 25 '15 at 08:23
  • @SouravGhosh Use search and find this answer, there are plenty of them on SO. Then you can update the answer if that bothers you. – this Jun 25 '15 at 08:33
  • @Sourav Ghosh How do you do, Sir.:) Please see my updated post. – Vlad from Moscow Jun 25 '15 at 08:36
  • @SouravGhosh Empty declaration lists have never been allowed in C. The syntax has always been the same throughout each version of the standard. – Lundin Jun 25 '15 at 09:30
0

sizeof returns the size of the pointer itself. In 64bit system it is 8 bytes. Pointers have no knowledge of the size of buffer they points to.

Igal S.
  • 13,146
  • 5
  • 30
  • 48
0

A pointer is a distinct data type and always has a fixd size. Think of a pointer as a sign that points to the actual data, and the sign always the same size - regardless if it points to a single character or a larger array.

Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
0

val is pointer and its size is equivalent to address bus size of system. so, sizeof(val) or sizeof(anyPointer) will give you 8 as output. if you want 40 then try sizeof(arr).

Pointer to an array is simple pointer where you can write val++ while array name is constant pointer you cannot write arr++.

you can also check this link

Community
  • 1
  • 1
NightWatcher
  • 148
  • 10
0

Most probably you are in a 64 bit PC where memory address need 64 bits or 8 byte space for representation.

Now the pointer actually holds the memory address, whereas it may hold an address of int or maybe an address of int[], doesn't matter.

So, when you execute,

printf("Size is %lu\n", sizeof(val)) ; 

it shows 8 as because the pointer holds address which need 64 bits space.

And

printf("Size of int is %lu\n", sizeof(int)) ;

this line just print the size of int which is 4 bytes.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41