int a[10]; sizeof(a+1) = sizeof pointer, why?

Posted on 2003-11-14
Medium Priority
Last Modified: 2010-04-15
suppose I declare an int a[10].
sizeof(a) gives the total number of bytes allocated to the array while sizeof(a+1) gives the size of pointer. why is it so?
Question by:vivekGupta
LVL 45

Expert Comment

ID: 9746010
sizeof(a) === compiler knows that it has allocated 10 ints to an array a ... hence you get sizeof 10 ints as answer

compiler treats a as a pointer ... pointer to first int of the array ... when you say a+1, it actually points to the second int ... compiler has not allocated any memory to an array at that location ... it sees it as an int pointer .. hence the results
LVL 45

Accepted Solution

sunnycoder earned 500 total points
ID: 9746033
When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:

// expre_sizeof_Operator.cpp
// compile with: /EHsc
#include <iostream>

size_t getPtrSize( char *ptr )
   return sizeof( ptr );

using namespace std;
int main()
   char szHello[] = "Hello, world!";

   cout  << "The size of a char is: "
         << sizeof( char )
         << "\nThe length of " << szHello << " is: "
         << sizeof szHello
         << "\nThe size of the pointer is "
         << getPtrSize( szHello ) << endl;
LVL 23

Expert Comment

ID: 9750857
Because of the interchangability of pointers and arrays in C, a+1 means the same thing as a[1].
So sizeof(a+1) is semantically equivalent to sizeof(a[1]) which is sizeof(int).  I suspect your
implementation has sizeof(int) == sizeof(pointer).

LVL 22

Expert Comment

ID: 9754068
sizeof() is not like a C function, it's specially parsed by the compiler.  It has to ber a special case, as sizeof() can accept either a variable, an expresion, or a type. Regular C functions can't.  So don't expect the usual C rules to apply to its parameter.  

when you say    sizeof( a ), the compiler thinks you mean "the size of array a".

when you say sizeof( a + 1 ), the compiler thinks this is a pointer expression and correctly returns the size of a pointer.

LVL 17

Expert Comment

ID: 9757783
> sizeof(a+1) is semantically equivalent to sizeof(a[1]) which is sizeof(int).  

No... sizeof(a+1) is equivalent to sizeof(int*)

It is easier to see this on a 32-bit system with short, because sizeof(short) is 2 and sizeof(short*) is 4.

Check out the following:
#include <stdio.h>

void f(short [10]);

int main()
short arr[10];

        printf("In main()...with arr as an automatic variable\n");
        printf("sizeof(arr) is %u\n",sizeof(arr));
        printf("sizeof(arr[1]) is %u\n",sizeof(arr[1]));
        printf("sizeof(arr+1) is %u\n",sizeof(arr+1));

void f(short arr[10])
        printf("\nIn f()...with arr as a param\n");
        printf("sizeof(arr) is %u\n",sizeof(arr));
        printf("sizeof(arr[1]) is %u\n",sizeof(arr[1]));
        printf("sizeof(arr+1) is %u\n",sizeof(arr+1));

You can see that arr[1] is treated as a short in main() and f().
You can see that arr is treated as an array of shorts in main(), but that f() only gets a reference to that array in C, because unlike structs, arrays are passed by reference in C, and arr is therefore treated as a pointer.
You can see that arr+1 is treated as a pointer in main() and f(). The +1 is pointer arithmetic.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question