Thread: Difference between expressions of pointers

  1. #1
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44

    Question Difference between expressions of pointers

    Could you explain to me what's the difference between these expressions?
    Code:
    p++;
    Code:
    (*p)++;
    Code:
    *(p++);
    (assuming p as a pointer)

    Thanks
    Last edited by ekosix; 05-19-2010 at 07:08 PM. Reason: fix title

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first adds one to the pointer itself (making it point somewhere else). The second adds one to the value pointed to by p. The third moves the pointer, then gets the (new) value pointed to.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Chennai
    Posts
    4
    Code:
    I think so the following example will solve ur doubts
    
    #include <stdio.h>
    
     main(){
      int *p;
      int c;
    
      c = 12;
      p = &c;
    
      printf("%d\n", *p);           // this will print the value which it points
      //  printf("%d\n", p++);    // this wil point to some location                                                                                    
      printf("%d\n", (*p)++);    // this will print the original value which it points and then it increments
      printf("%d\n", *(p++));    // this will print the one time original value and address of p gets incremented
      printf("%d\n", *(p++));    // since address of p gets incremented it will print some value in it 
      return 0;
    }
    --------------------
    the output is 
    12
    12
    13
    -1074905056

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    To clarify:
    Code:
      printf("%d\n", *(p++));    // this will print the one time original value 
                         and address of p gets incremented
      printf("%d\n", *(p++));    // since address of p gets incremented 
                         it will print some value in it
    p++ is post increment, which means after the expression is evaluated (++p is pre increment). So the first time p++ is called, p's address increases by the unit size of it's type, in this case an int. In the case of sivanihonjin's code, this is now out of bounds so this should not have been done. Subsequently accessing p's value may cause a segmentation fault. If not, you will get whatever value happens to be where p is pointing to.
    Last edited by MK27; 05-21-2010 at 07:28 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Chennai
    Posts
    4
    ThanKs for your suggestion:
    Yes its true.
    p's value may cause a segmentation fault. If not, you will get whatever value happens to be where p is pointing to.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To be pedantic, it's not the value that causes the segfault. It's the dereferencing of the pointer that causes it. (i.e.: trying to access something you shouldn't)


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. vector destruction question
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 01:20 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

Tags for this Thread