gpt4 book ai didi

c - 需要左值作为增量操作数错误指针

转载 作者:太空狗 更新时间:2023-10-29 15:38:30 26 4
gpt4 key购买 nike

我只是想了解 C 中的指针概念并遇到了这个错误。对于以下代码,我得到了“需要左值作为增量操作数”错误。请帮助我了解问题所在。

#include<stdio.h>

int main()
{
char *s[] = {"black", "white", "pink", "violet"};
++s; //error at this line
printf("%s\n", *s);
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}

最佳答案

s 是指向 char 的指针数组。数组名称是不可修改的左值。 ++s 正在修改不能修改的s

Array Names vs Pointer Variables :

As we have seen, when we declare an array, a contiguous block of memory is allocated for the cells of the array and a pointer cell (of the appropriate type) is also allocated and initialized to point to the first cell of the array. This pointer cell is given the name of the array. When memory is allocated for the array cells, the starting address is fixed, i.e. it cannot be changed during program execution. Therefore, the value of the pointer cell should not be changed. To ensure that this pointer is not changed, in C, array names may not be used as variables on the left of an assignment statement, i.e. they may not be used as an Lvalue. Instead, if necessary, separate pointer variables of the appropriate type may be declared and used as Lvalues.


推荐阅读: Is array name a pointer in C?

关于c - 需要左值作为增量操作数错误指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24415864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com