<分区>
我定义了一个常量字符串数组作为
static const char *Props[] = {"Cp", "Cv", "Mu", "H"};
执行以下命令时出现错误:
while(*Props) printf("%s\n", *Props++);
错误信息是:
C/test.c:38:45: error: lvalue required as increment operand
有人可以解释为什么我会收到此错误吗?
<分区>
我定义了一个常量字符串数组作为
static const char *Props[] = {"Cp", "Cv", "Mu", "H"};
执行以下命令时出现错误:
while(*Props) printf("%s\n", *Props++);
错误信息是:
C/test.c:38:45: error: lvalue required as increment operand
有人可以解释为什么我会收到此错误吗?
最佳答案
数组指示符是不可修改的左值。您可能无法更改它们。您需要的是以下内容
static const char *Props[] = {"Cp", "Cv", "Mu", "H"};
for ( size_t i = 0; i < sizeof( Props ) / sizeof( *Props ); i++ )
{
puts( Props[i] );
}
另一种方法是向数组添加标记值。例如
static const char *Props[] = {"Cp", "Cv", "Mu", "H", NULL };
for ( const char **s = Props; *s; s++ )
{
puts( *s );
}
关于c - 仅在 C 中使用指针打印字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914122/