什么是 C 中的指针解引用?

Sho*_*ani -3 c pointers dereference

我正在使用 c 学习递归,并且有一个关于数组的问题,例如:

void main(){
     char str[100];
     --snip--
     if(*str == 'c')
        count++;
     --snip--
}
Run Code Online (Sandbox Code Playgroud)

这里的 *str 是从指向字符串str 的第一个字符的指针中检索到的值。

我的问题是什么是指针取消引用,我可以执行以下操作:

--snip--
str+=1;
--snip--
Run Code Online (Sandbox Code Playgroud)

获取指向该字符串中另一个字符的下一个位置的新指针?

mch*_*mch 5

您不能增加数组,但可以增加指向数组元素的指针:

char str[100];
char *pointer = str; //points to the begin
pointer++; //points to the second element
*pointer = 'a'; //sets the second element
Run Code Online (Sandbox Code Playgroud)

str具有类型char [100],在某些表达式中它会衰减为(例如,行为类似于)指针,但它不是指针,您不能分配给str。str = ...或str+=1;无效,您只能将某些内容分配给数组的元素,而不是数组本身。

因此,您必须创建一个指向数组元素的指针,然后才能递增该指针。