lvalue需要作为增量操作数错误指针

我只是想了解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的指针数组.数组名称是不可修改的l值. 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?

点赞