By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

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

Siddarth777
P: 28
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4. int *a=5;
  5. int **p=&a;
  6. fflush(stdin);
  7. printf("%u,%u,%d",p,*p,**p);
  8. }
getting compiled perfectly but am getting
"segmentation fault" in runtime
please help
Sep 18 '10 #1

✓ answered by Siddarth777

thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help

Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
The problem is that the pointer a does not point anywhere valid.

You say it compiles perfectly, do you get no compiler warnings? If not you probably don't have you compiler warning level set high enough.

The reason I say this is that on line 4 you assign an int to an int *. This effectively makes a point at memory location 5, almost certainly not a valid location on your system.

p points to a

So when you try to print

p is valid, it is the address of a
*p is valid it is a, the address of an int
**p is not valid it is the thing that a points to, that is memory location 5, not the address of an allocate int and you get a segmentation fault (a memory address fault).

You can not take the address of a numerical constant like 5.
Sep 18 '10 #2

Siddarth777
P: 28
thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help
Sep 18 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
int *a=5;

does explicitly set the value of a to (the memory location) 5. There is nothing wrong in that apart from being a little strange.

The problem comes when you try to dereference a with *a, you try to access the memory location contained in a, in this case 5 and that is not a valid memory location, or a least one you do not have access rights to.
Sep 18 '10 #4

Expert 100+
P: 2,396
Your code has a point to whatever int is stored at address 0x0005. The segmentation fault occurs because the operating system has not granted your program permission to access this address. In addition, you might get a run-time alignment fault if your program runs on one of the many processors that don't allow ints to be at odd addresses.

Do you intend for a to point to an int whose value is 5? If so, then you want something like this:
Expand|Select|Wrap|Line Numbers
  1. int five = 5;
  2. int *a = &five;
Sep 18 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.