Is this not the same value?

This is a discussion on Is this not the same value? within the C Programming forums, part of the General Programming Boards category; I have just recently started to learn C. I have been playing around with different code to teach myself. I ...

  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,185
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21