Thread: Passing a string to and from a function.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Passing a string to and from a function.

    I am having a problem passing a string to a function and back up to main. I know there is a problem with where I have the pointers but I've played with it for a while and i cant seem to get it working...
    Any ideas?
    Thanks..

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    main()
    {
    
    void convert (char *ch1[]);
    
    char str[10];
    
        printf("Enter a word: ");
        gets(str);
    
    convert (&str);
        printf("Converted to: %s",&str);
    
    
    }
    
    void convert (char *ch1[])
    {
        
        char ch = ch1;
    
        ch= toupper(ch1);
    
        return ch;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have a string, you have an array of pointers. Drop either the * or the []. Then, pass just str not &str to the function.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You're using a old book or something aren't you?

    Before we get to the function itself, here's some problems you should fix:
    - return type of main is int, and it should have a return value.
    - don't declare function prototypes inside main()
    - don't use gets() for anything.

    So:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void convert (char *ch1);  // <--
    
    int main() {
    	char str[10];
    
    	printf("Enter a word: ");
    	fgets(str,10,stdin);
    
    	convert (str);
    	printf("Converted to: %s",str);  // no & here or above
    
    	return 0
    }
    Now see what you can do with convert. Notice I altered the prototype slightly as quzah described.

    Also: you probably want to use the return value of convert (that is, make it char*) since the changes made to str there will not apply in main.
    Last edited by MK27; 05-19-2010 at 03:06 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    well for one thing you are defining your function as a void return type (void means nothing for the slow kids) so returning ch isn't going to do anything. Didn't your compiler complain about that?

    In any event, convert your convert function like so:
    Code:
    void convert(char *str)
    {
       char *ptr = str;
       while(*ptr)
       {
          char ch = (*ptr);
          ch = toupper(ch);
          (*ptr) = ch;
          ++ptr;
      }
    
    
    }
    The rest is fine...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks...

    The code I'm learning is from a book but its not "that" old My lecturer wrote it so I have to use it..
    I have now taken out the pointers and left only the array. I still get the string I input in the output.. So either its not getting to the function or it is and its not being changed before going back to main??

    Code:
    void convert (char ch1[]);  
    
    int main() {
    	char str[10];
    
    	printf("Enter a word: ");
    	fgets(str,10,stdin);
    
    	convert (str);
    	printf("Converted to: %s",str);  
    
    	return 0;
    }
    void convert (char ch1[])
    {
    char ch = ch1;
    ch = toupper(ch1);
    return ch;
    }

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    oops... thanks i just saw your reply!!

    Thanks!

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I have gotten rid or the pointers as mentioned by Zuzah in favour of an array. I have changed the void to char and now I get 2 compiler errors

    Code:
    In function `convert':|
    initialization makes integer from pointer without a cast|
    passing arg 1 of `toupper' makes integer from pointer without a cast|
    Im still none the wiser?

    Code:
    char convert (char ch1[]);
    
    int main() {
    	char str[10];
    
    	printf("Enter a word: ");
    	fgets(str,10,stdin);
    
    	convert (str);
    	printf("Converted to: %s",str);
    
    	return 0;
    }
    char convert (char ch1[])
    {
    char ch = ch1;
    ch = toupper(ch1);
    return ch;
    }

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Obe View Post
    Im still none the wiser?
    This is going to take a little longer than you think, is all. Here's your two errors:

    test.c:20: warning: initialization makes integer from pointer without a cast
    test.c:21: warning: passing argument 1 of ‘toupper’ makes integer from pointer without a cast


    The first one is because this:
    Code:
    char ch
    is a single character, not a char pointer. A char pointer looks like this:
    Code:
    char *ch
    The second is because toupper works on one char at a time. See jeffcobb's post #4. Here's a variation on that which might help you understand:

    Code:
    	int i, len = strlen(ch1), c;
    	for (i=0;i<len;i++) {
    		c = toupper(ch1[i]);
    		ch1[i] = c;
    	}
    Last edited by MK27; 05-19-2010 at 03:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, you're basically converting a local variable, and then returning it (but not using the return value). What you probably want to do is loop through the array (until you hit the null-terminator), and then convert each cell into lowercase. Right? In that case, you can omit the return value altogether, too...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sebastiani View Post
    Also, you're basically converting a local variable, and then returning it (but not using the return value). What you probably want to do is loop through the array (until you hit the null-terminator), and then convert each cell into lowercase. Right? In that case, you can omit the return value altogether, too...
    Hey that's true, ch1 is not reassigned. My mistake. You don't need to use the return value after all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks for all your help... its working now! without pointers and using a void..Thankfully

Popular pages Recent additions subscribe to a feed

Tags for this Thread