Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now