Thread: Postfix incrementer and pointer question

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    9

    Postfix incrementer and pointer question

    I'm having a hard time understanding this short snippet of code. When you pass a pointer to a function and then use the postfix increment operator, why does the value of the pointer keep its original value after it exits the function?

    Code:
    int main(void)
    {
        char line[LIMIT];
    
        puts("Please enter a line:");
        gets(line);
        ToUpper(line);
        printf("%p \n", line);
        puts(line);
        printf("%p \n", line);
    
       return 0;
    }
    
    
     void ToUpper(char * str)
    {
        while (*str)
        {
            *str = toupper(*str);
            str++;
        }
    }
    Output:
    Please enter a line:
    testing...
    0x7fffffffea40
    TESTING...
    0x7fffffffea40

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Because you are only passing a copy of that pointer to your function. C is pass by value. In order to actually change the value of the pointer and have those changes be visible from the caller, you need to pass a pointer to your pointer. e.g. char**.

    EDIT:
    Quote Originally Posted by AndrewHunter View Post
    The confusion you are having comes down to really wrapping your head around 'pass by value'. Anything you pass to a function, including a pointer, is pass by value. Thus any changes made in that function are gone once the function returns. This includes making assignments to pointers, which is what you are doing in your second example.

    A simplier example of the same concept is :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(int* ptr){
    
    	ptr = malloc(sizeof(int));
    	*ptr = 5;
    }
    int main(void){
    
    	int* mainptr = NULL;
    	
    	if(mainptr==NULL)
    		printf("mainptr is NULL\n");
    	
    	foo(mainptr);
    	
    	if(mainptr==NULL)
    		printf("mainptr is NULL\n");
    
    	getchar();
    	return(0);
    }
    For a similar explanation, you can read c-faq Question 4.8
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    Perfect, thanks for the quick reply! That is what I suspected but I couldn't find the answer.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by albundy View Post
    Perfect, thanks for the quick reply! That is what I suspected but I couldn't find the answer.
    To see it more clearly... print out the value of your pointer just before you return from the function... It'll get plenty obvious then.

    Afterthought: Interestingly, it's a good thing that it does work that way, otherwise your program would have crashed.
    Last edited by CommonTater; 07-31-2011 at 05:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  2. Little question about postfix and prefix
    By firebird in forum C Programming
    Replies: 3
    Last Post: 05-08-2008, 07:27 AM
  3. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  4. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM