2011-03-28 2 views
22

('지능'이) :오류 : '단항 *'의 유효하지 않은 유형의 인수가 나는 C 프로그램을

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

누군가가 무엇을이 오류를 설명 할 수 :

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

컴파일러 오류가 발생합니다 방법?

답변

17

이 들고 정수 포인터의 주소. 형식은여야합니다.:

int **c; 
c = &a; 

전체 프로그램이됩니다 : 당신은 변수의 타입을 선언하면

#include <stdio.h>                
int main(){ 
    int b=10; 
    int *a; 
    a=&b; 
    int **c; 
    c=&a; 
    printf("%d",(**c)); //successfully prints 10 
    return 0; 
} 
+3

답안에 캐스트가 없다는 점에 유의하십시오. 질문에있는 캐스트는 'int **'를'int * '에 대입하고있는 라인의 문제를 숨 깁니다. ('c = (int *) & a;') – Thanatos

5

코드를 다시 포맷했습니다.

오류

이 라인에 위치시켰다 : * 표시는 주소 값을 검색

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

:

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

는로 변경 고쳐. ** 주소에서 다른 값의 값 (이 경우 주소)을 검색합니다.

또한()는 선택 사항입니다.

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

라인이 :

c = a; 

그것은 포인터 'C'의 값이 동일 의미 :

c = &a; 

의해 대체되어야 포인터 'a'의 값 따라서 'c'와 'a'는 같은 주소 ('b')를 가리 킵니다. 출력은 다음과 같습니다

10 

편집 2 :

당신이 이중 *를 사용하려면 :

#include <stdio.h> 

int main(void) 
{ 
    int b = 10; 
    int *a = NULL; 
    int **c = NULL; 

    a = &b; 
    c = &a; 

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

    return 0; 
} 

출력 : c 이후

10 
+0

나는 그 문제가 해결 모르겠습니다을, 인쇄 된 결과 "입니다 - 108149370 "이 아니고 10을 입력하십시오. – picstand

+0

예, EDIT를 읽으세요 ;-) 문제를 해결합니다. –

+0

예 10을 인쇄하는 Sandro가 있지만 목표는 이중 참조를 사용하여 b 값 (즉 10)을 인쇄하는 것입니다. – picstand

0

, 당신은 같은 유형으로 캐스팅 할 필요가 없습니다. 따라서 a=&b;을 작성할 수 있습니다. 마지막으로 c을 잘못 선언했습니다. 주소를 a으로 지정 했으므로, aint에 대한 포인터이며, int에 대한 포인터 포인터로 선언해야합니다.

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

예. 고마워. – picstand

10

는 베어 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 :

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. 
관련 문제