My function argument is being altered but why?

This is a discussion on My function argument is being altered but why? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define ARRSIZE 200 void itoa(signed char, char[]); void reverse(char[]); int main(void) { signed char num = -127; ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    My function argument is being altered but why?

    Code:
    #include <stdio.h>
    #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?

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    My output is:

    -127

    -127

    721-

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    /*ITOA converts signed int n into its character string 		*/
    /*equivalent in s						*/
    void itoa(signed char n, char s[]) {
    Why are you using char? any specific reason?

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Quote Originally Posted by Bayint Naung View Post
    Code:
    /*ITOA converts signed int n into its character string 		*/
    /*equivalent in s						*/
    void itoa(signed char n, char s[]) {
    Why are you using char? any specific reason?
    Either way, the number 127 is small enough.

    Code:
    /*==============================================================*/
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    For some reason, my eyes hate looking at those.


    Edit: You deleted the post... Anyway, using the format specifier %d on a character will just print the decimal equivalent instead of the characer value.
    Last edited by Syscal; 09-01-2010 at 12:51 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Edit: You deleted the post... Anyway, using the format specifier %d on a character will just print the decimal equivalent instead of the characer value.
    ya, that's not the point I'm saying.
    for variable arg function. char arg are promoted to int. So should be no problem.
    but for case like printf("%f", 3) will cause problem.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    It's nice that you're getting the right output Syscal...any ideas why I might not be?

    Thanks for your responses.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Quote Originally Posted by Kayl669 View Post
    It's nice that you're getting the right output Syscal...any ideas why I might not be?

    Thanks for your responses.
    The use of different compilers or compiler options.

    My results below using Luna MinGW GCC
    http://sourceforge.net/projects/lunac/
    -127

    -127


    -127
    My results below using TDM build of MinGW GCC "gcc version 4.5.0 (tdm-1)"
    -127

    -127


    721-


    Looks like an compiler bug; or an undefined condition that I do not see.

    Fix/Workaround for what is likely an Compiler Optimization bug.
    Replace
    Code:
    for ( ; s[j]!=0; ++j)
    with
    Code:
    for (j=0; s[j]!=0; ++j)
    Tim S.
    Last edited by stahta01; 09-02-2010 at 03:06 PM. Reason: add fix

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Oh I See! Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  4. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM

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