gpt4 book ai didi

C程序无法编译: it shows "lvalue" error

转载 作者:行者123 更新时间:2023-11-30 18:14:43 25 4
gpt4 key购买 nike

#include <stdio.h>
int main()
{
int ary[2][3];
foo(ary);
}
void foo(int (*ary)[3])
{
int i = 10, j = 2, k;
ary[0] = &i;
ary[1] = &j;
for (k = 0;k < 2; k++)
printf("%d\n", *ary[k]);
}

我用许多不同的编译器尝试过,但我不明白为什么它不能编译。

最佳答案

您正在分配给具有数组类型的表达式。

foo 内的

ary 是一个“指向三个 int 数组的指针”类型的变量。

当您分配给它时,您正在尝试将“指向 int 的指针”类型的变量分配给“三个 int 的数组”类型的变量.

这是无效的,因为数组是 not an lvalue (6.3.2.1p3):

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

关于C程序无法编译: it shows "lvalue" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59020121/

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