C 一元'的类型参数无效*';(具有';int';)

C 一元'的类型参数无效*';(具有';int';),c,pointers,gcc,C,Pointers,Gcc,我正在做以下的家庭作业: 我试图用gcc编译这段代码,但当我这样做时,它会给我以下错误: w03_3_prob15.c: In function 'main': w03_3_prob15.c:14:7: error: invalid type argument of unary '*' (have 'int') 我正在使用以下命令进行编译: gcc -o w03_3_prob15 w03_3_prob15.c -std=c99 我真的不知道该怎么办。关于如何修复此错误,您有什么想法吗?p2的

我正在做以下的家庭作业:

我试图用gcc编译这段代码,但当我这样做时,它会给我以下错误:

w03_3_prob15.c: In function 'main':
w03_3_prob15.c:14:7: error: invalid type argument of unary '*' (have 'int')
我正在使用以下命令进行编译:

gcc -o w03_3_prob15 w03_3_prob15.c -std=c99

我真的不知道该怎么办。关于如何修复此错误,您有什么想法吗?

p2的类型是
int*
a1[2]
的类型是
int
,因此
*a1[2]
没有意义。你确定你准确地抄袭了作业题吗?(如果是这样的话,就会出现糟糕的家庭作业问题。)

p2的类型是
int*
a1[2]
的类型是
int
,因此
*a1[2]
没有意义。你确定你准确地抄袭了作业题吗?(如果是这样的话,那就是糟糕的家庭作业问题。它们会发生。)

在C中,一元
*
仅为指针定义
p2
是一个
int*
a1
是一个
int[]
a1[2]
是一个
int
<代码>[]的优先级高于一元
*
,因此您有
*(a1[2])
,这不是一个合法的表达式。这就是编译器停止的原因

我可以想出两种可能的解决办法。你想要哪一个,取决于你想做什么

*p2 = a1[2]; // Assigns the value of the second int in the array to the location
             // pointed to by p2.
p2 = &a1[2]; // Assigns the location of the second int in the array to p2.
在C中,一元
*
仅为指针定义
p2
是一个
int*
a1
是一个
int[]
a1[2]
是一个
int
<代码>[]的优先级高于一元
*
,因此您有
*(a1[2])
,这不是一个合法的表达式。这就是编译器停止的原因

我可以想出两种可能的解决办法。你想要哪一个,取决于你想做什么

*p2 = a1[2]; // Assigns the value of the second int in the array to the location
             // pointed to by p2.
p2 = &a1[2]; // Assigns the location of the second int in the array to p2.

这一行没有编译,因为它在书中不正确。从:


这一行没有编译,因为它在书中不正确。从:


你不应该为了做作业而编辑它。你应该能够看到它并知道答案。但是你发布的代码被破坏了。回到书上,检查你打的是否正确。我只是检查了我从书上准确地键入了代码,我做到了。请注意,添加“第14行:”是为了显示错误所在的位置。您不需要编译它来完成作业。你应该能够看到它并知道答案。但是你发布的代码被破坏了。回到书上,检查你打的是否正确。我只是检查了我从书上准确地键入了代码,我做到了。请注意,添加“第14行:”是为了显示错误所在的位置。这就解决了它!谢谢你把那一页指给我看。这将在未来派上用场。这就解决了它!谢谢你把那一页指给我看。这将在未来派上用场。
*p2 = a1[2]; // Assigns the value of the second int in the array to the location
             // pointed to by p2.
p2 = &a1[2]; // Assigns the location of the second int in the array to p2.
Page 438, line 17 from the bottom.
p2 = *a1[2]; 
should be  p2 = &a1[2];