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

Posted on 2003-11-14
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 125 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coverting 24 hour time to 12 hour in C++ 15 178
smtp c source code 7 85
How to access ANSI/IEEE Std 754 or equivalent information ? 3 73
C qsort compare function issue 6 16
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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