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

后端 未结 4 2722
既然无缘
既然无缘 2020-12-24 13:27

I have a C Program:

#include 
int main(){
  int b = 10;             //assign the integer 10 to variable \'b\'

  int *a;                 //dec         


        
4条回答
  •  误落风尘
    2020-12-24 14:09

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

    10
    

    EDIT 2:

    If you want to use a double * :

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

    Output:

    10
    

提交回复
热议问题