Thread: Pointer incrementation

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Pointer incrementation

    Just after confusing myself on it and wrecking my head:


    Code:
    int x =6;
    int *p=&x
    
    printf("%d", (*p+6))   //gives me 12;
    printf("%d", *p)  //should give 12 also??
    
    if i did:
    printf("%d", (*p+=))   //gives me 7 should it?;

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    I am just a beginner at C so perhaps this is irrelevant.

    Code:
    int x =6;
    int *p=&x
    should you not initialise the pointer like this:

    Code:
    int x = 6;
    int *p;
    p = &x;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by newbie30 View Post
    printf("%d", *p) //should give 12 also??
    Why should it give you 12?

    if i did:
    printf("%d", (*p+=)) //gives me 7 should it?;
    += what?


    Quote Originally Posted by Tom Bombadil View Post
    I am just a beginner at C so perhaps this is irrelevant.

    Code:
    int x =6;
    int *p=&x
    should you not initialise the pointer like this:

    Code:
    int x = 6;
    int *p;
    p = &x;
    It doesn't matter how you initialize the pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Instead of asking about code that is obviously mistyped and couldn't possibly produce any output at all, why not post the real code that gives you this output.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 6;
        int* p = &i;
        
        printf("%d\n", *p + 6); /* 12 */
        printf("%d\n", *p);     /* 6 */
        printf("%d\n", ++*p);   /* 7  NB! *++p would be a serious error */
        printf("%d\n", *p);     /* 7 */
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by newbie30 View Post
    Just after confusing myself on it and wrecking my head:


    Code:
    int x =6;
    int *p=&x
    
    printf("%d", (*p+6))   //gives me 12;
    printf("%d", *p)  //should give 12 also??
    
    if i did:
    printf("%d", (*p+=))   //gives me 7 should it?;
    p is a pointer which points to x. Now *p will give the value of x, and *p+6 will give x+6 which is nothing but 12. Again *p is same as x thus 2nd printf prints 6 not 12.
    For the 3rd printf I'm assuming you meant *p++(forgot the shift key), since ++ and * have the same precedence and right to left associativity thus p++ is done first and then the value at that address which is nothing but value at the address where p was pointing, thus printing 6. But now p points to a different address, so a 4th printf will give *p=some garbage value.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by anon View Post
    Code:
        printf("%d\n", ++*p);   /* 7  NB! *++p would be a serious error */
    what's NB?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    NB: nota bene

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM