遍历指向字符串的指针数组 - "lvalue required as increment operand"

Stepping through an array of pointers to strings - "lvalue required as increment operand"

我对我要在这里说明的这个程序感到困惑。 我写了两个简单的程序来打印字符串列表。首先,我制作了一个指向字符串的指针数组。这就是我尝试做的方式

#include <stdio.h>

int main()
{
    int i = 2;
    char *a[] = {"Hello", "World"};

    while (--i >= 0) {
        printf("%s\n", *a++); // error is here.
    }

    return 0;

}

我需要它来打印

Hello
World

但是有编译错误,它说,

lvalue required as increment operand.

然后我把程序改成了下面的

#include <stdio.h>

void printout(char *a[], int n)
{
    while (n-- > 0)
        printf("%s\n", *a++);
}

int main()
{
    int i = 2;
    char *a[] = {"Hello", "World"};

    printout(a,i);
    return 0;

}

然后它按预期工作了。

我的问题是,将数组名称传递给函数时有何不同?为什么它第一次不工作(我怀疑 "array names cannot be modified" 是原因但是为什么在第二个程序中,它允许我增加)?

*a++

++ 要求其操作数是可修改的左值。

在第一个例子中,a是一个数组。在第二个示例中,当作为参数传递给函数时,数组衰减为指针(指向其第一个元素),因此代码可以编译。