Solved

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

Posted on 2003-11-14
7
589 Views
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?
0
Comment
Question by:vivekGupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
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
0
 
LVL 45

Accepted Solution

by:
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:

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;
}
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_sizeof_operator.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_clang_the_sizeof_operator.asp
http://pegasus.rutgers.edu/~elflord/cpp/gotchas/index.shtml
0
 
LVL 23

Expert Comment

by:brettmjohnson
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).

0
 
LVL 22

Expert Comment

by:grg99
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.

0
 
LVL 17

Expert Comment

by:rstaveley
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:
--------8<--------
#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));
        f(arr);
}

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));
}
--------8<--------

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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

705 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