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

144,634

Solution 1

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

Solution 2

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.

Solution 3

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
Share:
144,634
picstand
Author by

picstand

Updated on February 06, 2020

Comments

  • picstand
    picstand almost 3 years

    I have a C Program:

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

  • picstand
    picstand over 11 years
    I am not sure that solves the problem, the printed result is "-108149370" and not 10.
  • picstand
    picstand over 11 years
    yes Sandro that does print 10 then, but the objective is to use double dereference to print the value of b (i.e. 10).
  • David Heffernan
    David Heffernan over 11 years
    I think the **c was intentional.
  • Sandro Munda
    Sandro Munda over 11 years
    @picstand see my EDIT 2 ;) is it ok for you ?
  • Thanatos
    Thanatos about 9 years
    Also note the lack of casts in the answer. The casts in the question hide the problems on the line that is assigning an int ** to a int *. (c=(int *)&a;)