1

If I have the following pointer variable declarations:

int *a;
int **c;

Regarding to the type and what values each will hold, will it be as follows:

a is of type int*, and will hold a memory address

*a is of type int, and will hold the value of the variable the pointer is pointing to

c is of type int**, and will hold ?????????????????????

c* is of type int*, and will hold the memory address of the pointer it is pointing at

c** is of type int, and assuming that pointer c is pointing to pointer b, and pointer b is pointing to variable a, here, the value held will be the value of the variable a

Is it correct this way, except c which I'm not sure about?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 3
    You seem to mean `*c` and `**c`. `c*` and `c**` make no sense. – aschepler Jan 27 '11 at 17:53
  • It's important to consider that an expression `(*a)` after your code would be invalid, because you never actually told the pointer `a` to point anywhere. You never allocated memory for an `int`; only for the pointer. – Lightness Races in Orbit Jan 27 '11 at 18:54

7 Answers7

7
int *a;
int **c;

You are correct about a. It is more common to say a holds a pointer to int.

c is of type int** and will hold a pointer to a pointer to int.

*c is of type int*, and will hold a pointer to int.

**c is of type int, and will hold an integer value. You are correct about c pointing to b and b pointing to a.

See cdecl for some help. :)

James
  • 5,355
  • 2
  • 18
  • 30
  • When you say for example `a holds a pointer to int`, what is the value of `pointer`? Is it a `memory address` always? Isn't saying it hold a `memory address` much simpler? Thanks – Simplicity Jan 27 '11 at 18:53
  • 1
    @user588855: practically it is a memory address, but conceptually is *something* that can be used to locate an object of the appropriate type. In many architectures pointers don't hold real memory addresses (hardware address), but a different value that the hardware maps to the actual memory bank and address inside that bank (virtual address). If you think on transactional memory systems, the same pointer could be used to refer to different memory addresses before and after a transaction is committed, so it could be some unique identifier used to index a hashtable. – David Rodríguez - dribeas Jan 27 '11 at 19:07
2

c is of type int**, and will hold ?????????????????????

'c' also holds a memory address, just as 'a' does. The difference is that 'c', when dereferenced, will return another memory address. You're just adding another level of indirection.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
2

In your example, c will hold a pointer to an int*; that is, c is a pointer to a pointer. It can be used for multidimensional arrays (like matrices) and it can be used as a function parameter to change a user's int*.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
2

-- a is of type int*, and will hold a memory address

Correct

-- *a is of type int, and will hold the value of the variable the pointer is pointing to

Not exactly: *a will be a reference to the variable the adress points to. You see this when trying *a = 8; and int * x = &(*a)). If it was a value you couldn't change it. But since it is a reference the value is "routed" to the original place...in this case the memory a points to.

-- c is of type int**, and will hold ?????????????????????

c holds a memory address pointing to a memory address pointing to an int.

*c holds a reference to a memory adress pointing to an int. So you can do: *c = a;

**c is the same as *a.

mmmmmmmm
  • 15,269
  • 2
  • 30
  • 55
1

Every pointer holds memory address. In this case, c is a pointer to int* so, it will hold a memory address of such a variable.

Often, double pointers are used to create dynamic multiarrays in C. You can see it here

Community
  • 1
  • 1
finiteautomata
  • 3,753
  • 4
  • 31
  • 41
1

C is of type int*. As evident from that, it will hold a data of type int* which in itself is a memory address. This concept is called far pointer and there can be multiple far pointers up to a certain limit.

Like int** c, you can also have int*** d point to int** c. This is like a queue with every pointer pointing to the next pointer and the front having the data as the actual variable.

Chinmoy
  • 1,750
  • 2
  • 21
  • 45
0

The simplest thing that I can think of is playing with spaces. The compiler does not really care, but it makes reasoning easier due to the fact that the syntax of declarations and the usage are the same (by design):

Given:

int ***a; // declaration

int*** a; // type of a is an int*** (read: pointer to pointer to pointer to int)
int** *a; // the type of the object pointed by a (*a) is int**
int* **a; // the type of **a is int*
int ***a; // the type of ***a is int

Disclaimer: this is with respect to data types. Whether you can or not dereference a pointer at runtime is a different issue (has it been initialized, does it point to valid memory...?)

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489