1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My function argument is being altered but why?

Discussion in 'C' started by Kayl669, Sep 1, 2010.

  1. Kayl669

    Kayl669 New Member

    Joined:
    Sep 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #define ARRSIZE 200
    
            void itoa(signed char, char[]);
            void reverse(char[]);
    
    int main(void) {
            
            signed char num = -127;
            char string[ARRSIZE];
    
            printf("%d\n\n", num);
    
            itoa(num, string);
    
            printf("%d\n\n", num);
    
            printf("\n%s\n", string);
    
    return 0;}
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    /*ITOA converts signed int n into its character string                 */
    /*equivalent in s                                                */
    void itoa(signed char n, char s[]) {
    
            char i=0;
            signed sign;
    
            if ((sign = n) < 0)
                    n=-n;
            
            do {
                    s[i++] = n % 10 + '0';}
            while ((n/=10) > 0);
            
            if (sign < 0)
                    s[i++] = '-';
            
            s[i]=0;
    
            reverse(s);
    }
    /*==============================================================*/
    /*REVERSE reverses the ordering of values in string s                */
    void reverse(char s[ARRSIZE]) {
    
            char i=0, j;
            char temp[ARRSIZE];
                    
            while (s[i]!=0)
                    ++i;
    
            for (j=i-1, i=0; s[i]!=0; --j, ++i) 
                    temp[i] = s[j];
    
            for (; s[j]!=0; ++j) 
                    s[j]=temp[j];
    }
    As you can see, I've placed 2 printfs around my 'itoa' function call in main. According to my text book, the value displayed in both cases for the value of 'num' shouldn't differ because in C "all function arguments are passed 'by value.'" However it is -127 and 4 respectively. I'm confused! Can someone please demystify this situation for me please?
     

Share This Page