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

后端 未结 4 2709
既然无缘
既然无缘 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 13:59

    Since c is holding the address of an integer pointer, its type should be 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;
    }
    
    0 讨论(0)
  • 2020-12-24 14:05

    Barebones C program to produce the above error:

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

    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.

    0 讨论(0)
  • 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 <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 :

    10
    

    EDIT 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
    
    0 讨论(0)
  • 2020-12-24 14:15

    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 c incorrectly. 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.

    #include <stdio.h>
    int main(void)
    {
        int b=10;
        int *a=&b;
        int **c=&a;
        printf("%d", **c);
        return 0;
    } 
    
    0 讨论(0)
提交回复
热议问题