问题描述
我在指针符号*上苦苦挣扎,我发现它在声明和表达式中的使用方式非常混乱.
I'm struggling with the pointer sign *, I find it very confusing in how it's used in both declarations and expressions.
例如:
int *i; // i is a pointer to an int
但是语法背后的逻辑是什么?我之前的*是什么意思?让我们来看下面的例子.请纠正我错了的地方:
But what is the logic behind the syntax? What does the * just before the i mean? Let's take the following example. Please correct me where I'm wrong:
char **s;
char *(*s); // added parentheses to highlight precedence
这是我迷路的地方.括号之间的* s表示:s是一个指针?但是指针指向什么呢?括号外的*是什么意思:指向s的指针?
And this is where I lose track. The *s between the parantheses means: s is a pointer? But a pointer to what? And what does the * outside the parentheses mean: a pointer to what s is pointing?
所以它的意思是:指向s所指向的指针是指向char的指针?
So the meaning of this is: The pointer pointing to what s is pointing is a pointer to a char?
我很茫然. *符号在声明和表达式中的解释是否有所不同?如果是这样,对它有何不同的解释?我要去哪里错了?
I'm at a loss. Is the * sign interpreted differently in declarations and expressions? If so, how is it interpreted differently? Where am I going wrong?
推荐答案
c中的声明规则是,您以使用它的方式对其进行声明.
The rule of declaration in c is, you declare it the way you use it.
char *p
表示您需要*p
来获取字符,
char *p
means you need *p
to get the char,
char **p
表示您需要**p
来获取字符.
char **p
means you need **p
to get the char.
这篇关于如何理解C语言中的指针星号*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!