I just want to how this code is working
The output its giving me is 5.Code:main() { int *p = 5; printf("%d\n",p); }
I m using gcc on ubuntu.
I just want to how this code is working
The output its giving me is 5.Code:main() { int *p = 5; printf("%d\n",p); }
I m using gcc on ubuntu.
You assign the value 5 to p.
You print the value of p.
In a completely surprising twist of fate, 5 gets printed.
> I m using gcc on ubuntu.
So am I.
Your code is basically "I crossed the road blindfold, and I'm still alive - why is this?"Code:$ gcc -Wall foo.c foo.c:4: warning: return type defaults to ‘int’ foo.c: In function ‘main’: foo.c:5: warning: initialization makes pointer from integer without a cast foo.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’ foo.c:7: warning: control reaches end of non-void function
Whilst the answer may be mildly curious, perhaps even interesting, it certainly isn't useful.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
p is pointing to the int variable x.Code:main() { int *p,x=5; p=&x; printf("value of x is %d\n",*p); // printf("Address of x is %u\n",p); // i am printing the value of p here.but hey look i dont get 5. }
*p has the value x, which implies x as well as *p shud print 5,which they do.
if we print p,it prints the address of x.
hence,the question?
btw,nice sarcasm !!
I am getting the same warnings.but the chicken is running all over the road and is still alive and kicking.
and I dont know what u mean by it isnt useful.and yes its curious and thats the reason i asked why the hell its working. I didnt think it should work at all.
I have that option ON.but thanks anyway.
To all,
My query is why its working?it shud not.And if it is actually working then how?
its a 4 line program with nothing much to it.
btw,all of u have a great sense of humor and are good at sarcasm.its just that it didnt help me much.
i have had few queries earlier and got them resolved here.i really respect this forum.
Thanks All.
Yes, but that is totally different. You don't understand what you just said.p is pointing to the int variable x.
p holds the address of x, so it is right to say that p has the value of &x.
*p is not the value of p, it is the value of the object at the value of p. This is what indirect referencing is.*p has the value x, which implies x as well as *p shud print 5,which they do.
If you give p the value 5, it is the address 5. If then you print the value of p, you would get 5. Same story with &x. Therefore if we print p, it prints the address of x.
*p has an entirely different meaning. It means, retrieve the object stored at my value. That is why *p is the value of x.
It wasn't sarcasm, it was the answer.hence,the question?
btw,nice sarcasm !!
BTW nice back talk, idiot! Do that when you're right next time!
Last edited by whiteflags; 09-04-2011 at 12:18 AM. Reason: I quoted myself
Well you did it. However, it is dangerous, and you seem to be aware of the danger. And other people have pointed out that your compiler can tell you about the danger. There's nothing else to understand but what you are doing. The challenge of life itself but I am getting a bit philosophical.
> My query is why its working?it shud not.And if it is actually working then how?
You need to understand that C will let you do all sorts of stupid things that "seem" to work. But the "seem" is just a local effect of your OS/Compiler/current code/today. Even tiny things like a simple edit of the code can upset things.
Time back, compilers only ever complained about syntax errors, that code would have compiled without comment (and all it's far worse derivatives which dereferenced p), and the poor noob programmer was left wondering wtf happened.
Modern compilers like gcc can detect an awful lot of dumb stuff. Use them to help you write better programs by keeping the number of warnings at zero. Add "-Werror" to your compiler options.
Your "code" is basically
- pick randomly typed variable, and attempt to store a few bits which look like 00000101 at the binary level
- copy that variable to the stack (gee, I wonder whether my bits have been preserved)
- attempt to print whatever was copied, as a decimal integer
- hey, it "looks" like my original data! (surprise)
Now try it with a 64-bit integer (say 123456789012345UL), and a 32-bit pointer, and be "surprised" that it doesn't work.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
int *p is a pointer... it's just a variable, you can stuff whatever you want into it. There's no magic here. However if you were to use printf("%d", *p) you would find some wild number that just happened to be at the address you assigned. The only thing special about pointers is that they allow you to access a value at an address, instead of their own value...
There's no reason what you did won't work... C doesn't give a crap... its up to you to apply wisdom as a programmer.
Last edited by CommonTater; 09-04-2011 at 01:33 AM.