C语言 错误:“一元 *”的无效类型参数(有“int”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5455866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: invalid type argument of ‘unary *’ (have ‘int’)
提问by picstand
I have a C Program:
我有一个 C 程序:
#include <stdio.h>
int main(){
int b = 10; //assign the integer 10 to variable 'b'
int *a; //declare a pointer to an integer 'a'
a=(int *)&b; //Get the memory location of variable 'b' cast it
//to an int pointer and assign it to pointer 'a'
int *c; //declare a pointer to an integer 'c'
c=(int *)&a; //Get the memory location of variable 'a' which is
//a pointer to 'b'. Cast that to an int pointer
//and assign it to pointer 'c'.
printf("%d",(**c)); //ERROR HAPPENS HERE.
return 0;
}
Compiler produces an error:
编译器产生错误:
error: invalid type argument of ‘unary *' (have ‘int')
Can someone explain what this error means?
有人可以解释这个错误是什么意思吗?
采纳答案by codaddict
Since cis holding the address of an integer pointer, its type should be int**:
由于c它持有一个整数指针的地址,它的类型应该是int**:
int **c;
c = &a;
The entire program becomes:
整个程序变成:
#include <stdio.h>
int main(){
int b=10;
int *a;
a=&b;
int **c;
c=&a;
printf("%d",(**c)); //successfully prints 10
return 0;
}
回答by Eric Leschinski
Barebones C program to produce the above error:
准系统C程序产生上述错误:
#include <iostream>
using namespace std;
int main(){
char *p;
*p = 'c';
cout << *p[0];
//error: invalid type argument of `unary *'
//peeking too deeply into p, that's a paddlin.
cout << **p;
//error: invalid type argument of `unary *'
//peeking too deeply into p, you better believe that's a paddlin.
}
ELI5:
ELI5:
The master puts a shiny round stone inside a small box and gives it to a student. The master says: "Open the box and remove the stone". The student does so.
大师将一块闪亮的圆石放在一个小盒子里,交给一个学生。师曰:“开盒取石”。学生这样做。
Then the master says: "Now open the stone and remove the stone". The student said: "I can't open a stone".
然后上师说:“现在打开石头,取出石头”。学生说:“我打不开石头”。
The student was then enlightened.
这位学生当时就开悟了。
回答by Sandro Munda
I have reformatted your code.
我已经重新格式化了你的代码。
The error was situated in this line :
错误位于这一行:
printf("%d", (**c));
To fix it, change to :
要修复它,请更改为:
printf("%d", (*c));
The * retrieves the value from an address. The ** retrieves the value (an address in this case) of an other value from an address.
* 从地址中检索值。** 从地址中检索另一个值的值(在本例中为地址)。
In addition, the () was optional.
此外,() 是可选的。
#include <stdio.h>
int main(void)
{
int b = 10;
int *a = NULL;
int *c = NULL;
a = &b;
c = &a;
printf("%d", *c);
return 0;
}
EDIT :
编辑 :
The line :
线路:
c = &a;
must be replaced by :
必须替换为:
c = a;
It means that the value of the pointer 'c' equals the value of the pointer 'a'. So, 'c' and 'a' points to the same address ('b'). The output is :
这意味着指针“c”的值等于指针“a”的值。因此,“c”和“a”指向同一个地址(“b”)。输出是:
10
EDIT 2:
编辑2:
If you want to use a double * :
如果你想使用双 * :
#include <stdio.h>
int main(void)
{
int b = 10;
int *a = NULL;
int **c = NULL;
a = &b;
c = &a;
printf("%d", **c);
return 0;
}
Output:
输出:
10
回答by David Heffernan
Once you declare the type of a variable, you don't need to cast it to that same type. So you can write a=&b;. Finally, you declared cincorrectly. Since you assign it to be the address of a, where ais a pointer to int, you must declare it to be a pointer to a pointer to int.
一旦声明了变量的类型,就不需要将其强制转换为相同的类型。所以你可以写a=&b;. 最后,你声明c错误。由于您将其分配为 的地址a,其中a是指向 的指针int,因此您必须将其声明为指向 的指针int。
#include <stdio.h>
int main(void)
{
int b=10;
int *a=&b;
int **c=&a;
printf("%d", **c);
return 0;
}

