Mahesha999
Mahesha999

Reputation: 24931

Size of pointer, pointer to pointer in C

How can I justify the output of the below C program?

#include <stdio.h>

char *c[] = {"Mahesh", "Ganesh", "999", "333"};
char *a;
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;

int main(void) {
    printf("%d %d %d %d ",sizeof(a),sizeof(c),sizeof(cp),sizeof(cpp));
    return 0;
}

Prints

4 16 16 4 

Why?

Here is the ideone link if you want to fiddle with it.

Upvotes: 3

Views: 6029

Answers (4)

Kun Ling
Kun Ling

Reputation: 2219

a is an usually pointer, which represents the memory address. On 32-bit operating system, 32bit (4 Byte) unsigned integer is used to represent the address. Therefore, sizeof(a) is 4.

c is an array with 4 element, each element is a pointer, its size is 4*4 = 16

cp is also an array, each element is a pointer (the first *, wich point to another pointer (the second *). The later pointer points to an string in the memory. Therefore its basic element size should represent the size of a pointer. and then sizeof(cp) = 4*4 = 16.

cpp is a pointer's pointer's pointer. It is as well represent the 32bit memory address. therefore its sizeof is also 4.

Upvotes: 3

Keith Thompson
Keith Thompson

Reputation: 263647

char *c[] = {"Mahesh", "Ganesh", "999", "333"};

c is an array of char* pointers. The initializer gives it a length of 4 elements, so it's of type char *[4]. The size of that type, and therefore of c, is 4 * sizeof (char*).

char *a;

a is a pointer of type char*.

char **cp[] = {c+3, c+2, c+1, c};

cp is an array of char** pointers. The initializer has 4 elements, so it's of type char **[4]. It size is 4 * sizeof (char**).

char ***cpp = cp;

cpp is a pointer to pointer to pointer to char, or char***. Its size is sizeof (char***).

Your code uses %d to print the size values. This is incorrect -- but it happens to work on your system. Probably int and size_t are the same size. To print a size_t value correctly, use %zu -- or, if the value isn't very large, you can cast it to int and use %d. (The %zu format was introduced in C99; there might still be some implementations that don't support it.)

The particular sizes you get:

sizeof a == 4
sizeof c == 16
sizeof cp == 16
sizeof cpp == 4

are specific to your system. Apparently your system uses 4-byte pointers. Other systems may have pointers of different sizes; 8 bytes is common. Almost all systems use the same size for all pointer types, but that's not guaranteed; it's possible, for example, for char* to be larger than char***. (Some systems might require more information to specify a byte location in memory than a word location.)

(You'll note that I omitted the parentheses on the sizeof expressions. That's legal because sizeof is an operator, not a function; its operand is either an expression (which may or may not be parenthesized) or a type name in parentheses, like sizeof (char*).)

Upvotes: 3

tysonbul
tysonbul

Reputation: 11

So the reason you got 4 16 16 4, is because 'a' is simply a pointer, on its own, which only requires 4 bytes (as a pointer is holding a 32bit address depending on your architecture) and so when you have a **pointer which is == to a *pointer[], your really making an array of pointers, and since you initalized 4 things that created 4 pointers, thus the 4x4 = 16. And for the cpp you may ask "well wouldn't it then be 16 as it was initalized?" and the answer is no, because a ***pointer is its own separate variable and still just a pointer(a pointer to a pointer to a pointer, or a pointer to an array of pointers), and requires only 4bytes of memory.

Upvotes: 0

user1723095
user1723095

Reputation: 1219

a is a pointer. cpp is also a pointer just to different type (pointer to pointer to pointer). Now c is an array. You have 4 elements, each is a pointer so you have 4 * 4 = 16 (it would be different if you would run it on x64). Similar goes for cp. Try changing type to int and you will see the difference.

Upvotes: 2

Related Questions