#include <stdio.h>
void printa(char *a[])
{
for (int i = 0; i < 3; ++i) {
printf("%s\n", *a);
a++;
}
}
int main(void)
{
char *a[] = {"The first", "The second", "The third"};
for (int i = 0; i < 3; ++i) {
printf("%s\n", *a);
a++; // error: cannot increment value of type 'char *[3]'
}
printa(a); //OK
return 0;
}
因此,我的问题是为什么在主函数中的代码
a++
会导致编译错误(错误:无法增加类型为'char *[3]'的值)。但是如果我将指针数组a传递给函数printa
并在该指针上调用a++
,它就能完美地工作。谢谢。