• C

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

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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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).

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.

> 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.