Thread: pointer problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    pointer problem

    I just want to how this code is working

    Code:
    main()
    {
    int *p = 5;
    printf("%d\n",p);
    }
    The output its giving me is 5.
    I m using gcc on ubuntu.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You assign the value 5 to p.

    You print the value of p.

    In a completely surprising twist of fate, 5 gets printed.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I m using gcc on ubuntu.

    So am I.
    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
    Your code is basically "I crossed the road blindfold, and I'm still alive - why is this?"
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by *al View Post
    I just want to how this code is working

    Code:
    main()
    {
    int *p = 5;
    printf("%d\n",p);
    }
    The output its giving me is 5.
    I m using gcc on ubuntu.
    You'd know why if you were to turn on all "-Wall" warnings when compiling.
    Last edited by itCbitC; 09-03-2011 at 12:42 PM. Reason: oopsie!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    Your code is basically "I crossed the road blindfold, and I'm still alive - why is this?"
    The chicken was in a good mood.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You assign the value 5 to p.

    You print the value of p.

    In a completely surprising twist of fate, 5 gets printed.
    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 is pointing to the int variable x.
    *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 !!

    Quote Originally Posted by Salem View Post

    So am I.
    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
    Your code is basically "I crossed the road blindfold, and I'm still alive - why is this?"
    Whilst the answer may be mildly curious, perhaps even interesting, it certainly isn't useful.
    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.

    Quote Originally Posted by itCbitC View Post
    You'd know why if you were to turn on all "-Wall" warnings when compiling.
    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.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    p is pointing to the int variable x.
    Yes, but that is totally different. You don't understand what you just said.
    p holds the address of x, so it is right to say that p has the value of &x.
    *p has the value x, which implies x as well as *p shud print 5,which they do.
    *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.
    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.

    hence,the question?
    btw,nice sarcasm !!
    It wasn't sarcasm, it was the answer.
    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

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by whiteflags View Post
    If you give p the value 5, it is the address 5. If then you print the value of p, you would get 5.
    so u r saying that i am giving p 5 as its address.and then i am printing 5? I dont think thats a legal statement .i cant just assign an address value to whatever i please.address is fixed.aint it?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by *al View Post
    so u r saying that i am giving p 5 as its address.and then i am printing 5? I dont think thats a legal statement .i cant just assign an address value to whatever i please.address is fixed.aint it?
    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.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by *al View Post
    so u r saying that i am giving p 5 as its address.and then i am printing 5? I dont think thats a legal statement .i cant just assign an address value to whatever i please.address is fixed.aint it?
    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.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    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.
    Stack!! I guess that solves it.Thats what i was wondering too.but i wasnt sure if this is what is really happening.
    and hey thanks for the insight and sorry for the trouble!

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    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.
    Commontater: Dude..i got a lil confused with that but Thanks yet again!

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by *al View Post
    Commontater: Dude..i got a lil confused with that but Thanks yet again!
    The point was that C does not apply wisdom to pointers in any special way... their correct usage is up to you as a programmer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM