-1

I am very new to C and currently having some trouble with pointers and I am not sure if my logic is correct on this question clarification would be great.

Is the second expression legal? Why or Why not? What does it mean?

int** x = ....;
... **x ...

This is all the question gives and I came up with the following answer (I think its in the ballpark)

The int** x will initialize a pointer x to whatever address/value that is after the equal sign. **x ... will dereference the pointer to a value/variable

The question link that was proposed in the edit was just showing the difference between int* p and int *p which is nothing what i asked i understand this already.

Tom
  • 49
  • 5

2 Answers2

0

int *x is a pointer to int. int** y defines y as a pointer to pointer to int, so it points to a pointer, and the latter points to an int. Example:

int n = 42;
int* x = &n; // points to n
int** y = &x; // points to x, which points to n

In this case y is a pointer to pointer. First indirection *y gives you a pointer (in this case x), then the second indirection dereferences the pointer x, so **y equals 42.

Using typedefs makes everything looks simpler, and makes you realize that pointers are themselves variables as any other variables, the only exception being that they store addresses instead of numbers(values):

typedef int* pINT;

int n = 42;
pINT x = &n;
pINT* y = &x; // y is a pointer to a type pINT, i.e. pointer to pointer to int
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

If you use double * you are telling the compiler you want a pointer pointing to another pointer:

int x = 4;
int *pointer = &x;
int **pointer_pointer = &pointer;
printf("%d\n",**pointer_pointer); // This should print the 4
lilezek
  • 6,976
  • 1
  • 27
  • 45