错误:’unary *’的无效类型参数(有’int’)

我有一个C程序:

#include  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; } 

编译器产生错误:

 error: invalid type argument of 'unary *' (have 'int') 

有人可以解释这个错误的含义吗?

由于c保存整数指针的地址,因此其类型应为int**

 int **c; c = &a; 

整个程序变成:

 #include  int main(){ int b=10; int *a; a=&b; int **c; c=&a; printf("%d",(**c)); //successfully prints 10 return 0; } 

Barebones C程序产生上述错误:

 #include  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:

 You have a big plasma TV cardboard box that contains a small Jewelry box that contains a diamond. You asked me to get the cardboard box, open the box and get the jewelry box, open the jewelry box, then open the diamond to find what is inside the diamond. "I don't understand", Says the student, "I can't open the diamond". Then the student was enlightened. 

我重新格式化了你的代码。

错误位于此行:

 printf("%d", (**c)); 

要修复它,请更改为:

 printf("%d", (*c)); 

*从地址中检索值。 **从地址中检索另一个值的值(在本例中为地址)。

另外,()是可选的。

 #include  int main(void) { int b = 10; int *a = NULL; int *c = NULL; a = &b; c = &a; printf("%d", *c); return 0; } 

编辑:

这条线:

 c = &a; 

必须替换为:

 c = a; 

这意味着指针’c’的值等于指针’a’的值。 因此,’c’和’a’指向相同的地址(’b’)。 输出是:

 10 

编辑2:

如果你想使用双*:

 #include  int main(void) { int b = 10; int *a = NULL; int **c = NULL; a = &b; c = &a; printf("%d", **c); return 0; } 

输出:

 10 

一旦声明了变量的类型,就不需要将它强制转换为相同的类型。 所以你可以写a=&b; 。 最后,你错误地声明了c 。 由于您将其指定为a的地址,其中a是指向int的指针,因此必须将其声明为指向int的指针。

 #include  int main(void) { int b=10; int *a=&b; int **c=&a; printf("%d", **c); return 0; }