Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

pointers

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?

like image 496
picstand Avatar asked Mar 28 '11 07:03

picstand


1 Answers

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; } 
like image 143
codaddict Avatar answered Sep 22 '22 15:09

codaddict