开发者

C++ pointers - What values are held here?

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 开发者_StackOverflow中文版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.


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. :)


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.


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*.


-- 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.


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


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.


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...?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜