代码
Code
short **p = (short **)malloc(sizeof(short *)); *p = malloc(sizeof(short)); **p = 10; printf("**p = %d", **p);输出
** p = 10
**p = 10
在此代码中,声明了多指针**p,并且使用了*p而没有任何声明(可能是**p).
In this code, a multiple pointer **p is declared and *p is used without any declaration(maybe it's by **p).
在我的情况下,*p是什么意思?对不起,一个非常简单的问题.
What does *p mean in my case? Sorry for very simple question.
我看到了C标准和堆栈溢出,但是我找不到东西.
I saw C standard and stack overflow, but I couldn't find out something.
推荐答案对于任何数组或指针p和索引i,表达式p[i]完全等于*(p + i)(其中*是一元解引用运算符,它在指针上的结果就是指针指向的值.
For any array or pointer p and index i, the expression p[i] is exactly equal to *(p + i) (where * is the unary dereference operator, the result of it on a pointer is the value that the pointer is pointing to).
因此,如果我们有p[0],则它精确等于*(p + 0),等于*(p),等于*p.从此倒退,*p等于p[0].
So if we have p[0] that's then exactly equal to *(p + 0), which is equal to *(p) which is equal to *p. Going backwards from that, *p is equal to p[0].
所以
*p = malloc(sizeof(short));等于
p[0] = malloc(sizeof(short));还有
**p = 10;等于
p[0][0] = 10;(**p等于*(*(p + 0) + 0)等于*(p[0] + 0)等于p[0][0])
请务必注意,星号*在不同的上下文中可能表示不同的意思.
It's important to note that the asterisk * can mean different things in different contexts.
可以在声明变量时使用,然后表示声明为指针":
It can be used when declaring a variable, and then it means "declare as pointer":
int *p; // Declare p as a pointer to an int value它可以用来取消引用一个指针,以获取该指针指向的值:
It can be used to dereference a pointer, to get the value the pointer is pointing to:
*p = 0; // Equal to p[0] = 0它可以用作乘法运算符:
And it can be used as the multiplication operator:
r = a * b; // Multiply the values in a and b, store the resulting value in r更多推荐
当已经声明** p时,* p是什么意思
发布评论