0
int x=10;
int *y=&x;

int *z= &y;

printf("%d\n",*(*z));

I want to understand why we need int **z? What is wrong here?

3
  • 7
    In the code, you wrote int *z; but it should be int **z;. Typo?
    – DarkDust
    Jun 6, 2013 at 15:49
  • I doubt the int *z is a transcription error: rather, OP, is using z to store a different pointer type than declared. Jun 6, 2013 at 16:25
  • kandelvijaya -- What compiler did you use and did it give you any warnings? Jun 6, 2013 at 16:26

7 Answers 7

3

Here's a handy table showing the types of various expressions based on your declarations:

Expression        Type        Value
----------        ----        -----
         x        int            10
        &x        int *       address of x
         y        int *       address of x
        &y        int **      address of y 

Since the type of the expression &y is int **, you need to declare z as int ** to hold that value (and thus the expression &z would have type int ***).

So, why does it matter? A pointer is a pointer is a pointer, right?

Well, not necessarily. Pointers to different types may have different sizes and representations. From the online 2011 standard:

6.2.5 Types
...
28 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.

In the desktop and server world, most architectures are such that all pointers types have the same size and representation, but there are plenty of oddball architectures where that isn't true. You can have a word-addressed architecture where multiple char values are packed into a single word; a char * would need to have a couple of extra bits to index into that word to get to a specific char value. You can have a Harvard architecture where you have fewer address lines for data than you do for code (or vice versa), so pointers to object types and pointers to function types will have different sizes.

There's another issue at play here: pointer arithmetic. Since y points to an object of type int, the expression y++ will advance y to point to the next object of type int. Since z points to an object of type int *, the expression z++ will advance z to point to the next object of type int *. If int and int * have different sizes, then y and z will be advanced by different amounts.

In short, type matters, even for pointers.

1
  • 1
    Nice mention of the fact that representation can differ too.
    – JAB
    Jun 6, 2013 at 16:30
2

Let's take it a step at a time, shall we?

int x = 10;

This code creates an int variable at some memory location and sets its value to 10. This variable can be accessed by the convenient name x.

int *y=&x;

This code creates a pointer to an int variable at some memory location and sets its value tot he address of the x variable. This variable can be accessed by the convenient name y. If you printed the value or y you would get the address of x. If you dereferenced y (by doing *y) and printed the result then you would get 10

int *z= &y; // error

This won't compile: z is a of type pointer to int. Since y is already a pointer to int, when you take its address (by doing &y) what you get back is a pointer to pointer to int. That's why you need the double star.

int **z = &y; 

Now this will compile. This code creates a pointer to a pointer to int variable and sets it value to the address of the variable y. If you printed the value z now, you would get get the address to y. If you dereferenced z (by doing *z) and printed the result you would get the address of x.

2

x is a variable of type int, meaning it holds an integer value. The type of 10 is int.

y is a variable of type int*, meaning it holds a value representing a memory address at which a value of type int is stored. The unary operator & produces the memory address of its argument, and generally the type information is included along with that, so as x is an int the type of the address of whatever is held by x will be int*.

z is a variable of type int**, meaning it holds a value representing the address of a variable of type int*. &y produces the memory address of y, and thus the result is of type int**.


The reason why the type information is included is not just a matter of strict/static typing; the way pointer arithmetic is performed (e.g. p++, equivalent to p += 1) will differ depending on the size of the type being pointed to. So for a pointer to a 4-byte value, incrementing the pointer by 1 will result in a pointer to the location four bytes after the original, while for a pointer to a 1-byte value, incrementing the pointer by 1 will result in a pointer to the location one byte after the original. Essentially, adding an integer to a pointer results in pointer_value + integer*sizeof(underlying_type).

You can, of course, use inline casts on the pointer type to alter the factor on the integer, but that's a topic for another time.

2
  • -1, z is of type int * -- this is the essence of what OP is confused about. He is using an int * as an int **. Jun 6, 2013 at 16:24
  • He asked why int z** is required, meaning he knew it's not supposed to be int *z but didn't understand why.
    – JAB
    Jun 6, 2013 at 16:26
1

You need a double star because of this.

x = 10 (the memory at the address of x holds hte value 10).

y = the address of x. If you dereferenced y (*y), it would show you 10. If you printed the value at the address of y, it would show you the address of x. If you print the address of y, it will show you something different from these 3 things.

z = to address of y. Remember, when you dereference z now (*z), it is going to give you the value held at y, which is the address of x. When you double dereference this, it gives you the value at address of x, which is what you want (hence, why you need the double dereference).

1

In c/c++, it's helpful to read variable declarations from right to left in order to get a better understanding of what their types are. In your code segment, this would produce the following description:

x is an int

y is a pointer to an int

z is a pointer to an int

In the first line, you're assigning 10 to x. Since 10 is an int, this succeeds without problems.

In the second line, you're assigning the address of x to y. Since pointers hold addresses, x is an int and y is a pointer to an int, this line succeeds as well.

The third line is where you hit your snag. You're assigning the address of y to z. z is expecting an address to an int by it's getting the address of a pointer to an int.

As DardDust mentioned, you need to change your definition of z so that it's a pointer to a pointer to an integer.

int **z = &y;

Once you've got that, it will be easier to understand the last line and your first question.

the printf function is expecting an int but you're using z. In order to get the int value, you need to do a double dereference. The first dereference will return a pointer to an int, the second the int itself.

1
  • This is a good answer. One wonders why the assignment of int *z didn't cause a warning... Jun 6, 2013 at 16:26
0
  • x is a variable of type int which is stored somewhere in memory.
  • &x returns the address of the memory location where x is stored. That's a pointer. Thus the type returned by &x is int *. Now, this pointer is again stored somewhere in memory, using variable y.
  • And then you want to get the address of z via &y. The variable y already stores a pointer, so now you have a pointer to a pointer to an int, that is a int **.
  • In the printf statement, you then need to crawl that chain back:
    • The inner *z reads the memory pointed to by z. Since z has the memory location of y, *z return the memory location of y.
    • And then you read the memory pointed to by y (indirectly). And y points to x, so you end up reading x.

A bit mind twisting, but vital to understand when working with C.

0

For every time you need a pointer to you need to add a * to your type:

int x=10;

declares an int and assigns to it the value 10

-> no a pointer to so no *

int *y=&x;

declares a pointer to an int and saves and assigns to it the address of where x is stored

-> 1 a pointer to so 1 *

and finally

int **z= &y;

declares a pointer to a pointer to an int and assigns of it the address where y is stored

-> 2 a pointer to so 2 *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.