Thread: Is this not the same value?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    22

    Question Is this not the same value?

    I have just recently started to learn C. I have been playing around with different code to teach myself. I cannot figure out the following code:

    Code:
    #include <stdio.h>
    
    void testfunction (int *);
    
    void testfunction2 (int *);
    
    int main (void)
    {       
            
            int number = 5, other_number = 5;
            
            testfunction (&number);
            
            printf("\nnumber in main: %d\n\n", number);
            
            testfunction2 (&other_number);
            
            printf("\nother_number in main: %d\n\n", other_number);
            
            return(0);
     
    }
    
    
    void testfunction (int *value)
            
    {       
            
            *value++;
            
            printf("\nnumber in testfunction: %d\n", *value);
            
            return;
     
    }
    
    
    void testfunction2 (int *value)
    
    {
    
            *value += 1;
    
            printf("\nother_number in testfunction2: %d\n", *value);
    
            return;
    
    }

    For some reason when I run the above program I get this:

    Code:
    number in testfunction: -1077941616
    
    number in main: 5
    
    
    other_number in testfunction2: 6
    
    other_number in main: 6
    Shouldn't both numbers pointers be 6?

    Why am I getting this crazy number? Can I not use "++" outside of a loop? "+=" seems to work just fine.


    Thanks for the help!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try (*value)++ instead of *value++. (Hooray order of operations.)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    yep, that was it.

    Thanks!

Popular pages Recent additions subscribe to a feed