[英]error: invalid type argument of ‘unary *’ (have ‘int’)
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? 有人可以解释这个错误的含义吗?
Since c
is 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;
}
Barebones C program to produce the above error: Barebones 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. 然后学生开悟了。
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'. 这意味着指针'c'的值等于指针'a'的值。 So, 'c' and 'a' points to the same address ('b').
因此,'c'和'a'指向相同的地址('b')。 The output is :
输出是:
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
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;
所以你可以写
a=&b;
. 。 Finally, you declared
c
incorrectly. 最后,你错误地声明了
c
。 Since you assign it to be the address of a
, where a
is a pointer to int
, you must declare it to be a pointer to a pointer to int
. 由于您将其指定为
a
的地址,其中a
是指向int
的指针,因此必须将其声明为指向int
的指针。
#include <stdio.h>
int main(void)
{
int b=10;
int *a=&b;
int **c=&a;
printf("%d", **c);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.