你有指针和值:
int* p; // variable p is pointer to integer type
int i; // integer value
将指针转换为带*的值:
int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to
您可以将值转换为带& ;:的指针
int* p2 = &i; // pointer p2 will point to the address of integer i
编辑:
在数组的情况下,它们非常类似于指针。如果你认为它们是指针,你将使用*来获取其中的值,如上所述,但是还有另一个更常见的方法使用[]运算符:
int a[2]; // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element
获取第二个元素:
int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element
所以[]索引操作符是一个特殊形式的*运算符,它的工作原理如下:
a[i] == *(a + i); // these two statements are the same thing