Thread: Pass a string to a function

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    Pass a string to a function

    Hi Guys,

    I got stuck im my last thread so I have tried to simplify things to see where im going wrong,

    In class we just covered basic C functions, I can pass an 'int' back and forth no problems but im getting stuck passing a "string".

    This is not class work just me trying to get my head round learning basic C.

    I dont know about pointers as yet this has not been covered and I dont think it will be at the level we are at so im trying to do this without pointers.

    I want to take user input in the main() function and pass it to another function, manipulate the string in some way(in this case just add ABC to the string passed) and pass it back to the main() function and print it out with the new string3 added to the end of what the user input.

    Any help is appreciated.
    regards
    c

    Im using Borland 4.5 C++ Complier on WinXP, and its giving me the error:

    Cannot convert 'char *' to 'char' in function main()

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include <ctype.h>
    
    /*Functions Prototype */
    void check_str(char);
    
    
    /*The main program*/
    
    void main(void)
    {
    char string1[40];
    char answer[40];
    
    
      printf("\n\n\t\t   Enter string1\n\n");
      printf("Please enter a string \n");
      gets(string1);
    
    
      strcpy(answer,check_str(string1));
      printf("answer = %s",answer);
    
    }
    
    
    check_str(char string2)
    {
    char string2[40];
    char string3[40];
    string3[4]={'a','b','c','\0'};
    
    
        strcat(string2,string3);
       return char string2[40];
    }





    it trips on this line of code

    Code:
    strcpy(answer,check_str(string1));

  2. #2
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    >>void main(void)
    Always use int main()

    >>void check_str(char);
    As you defined this function, it should retun a string, and it gets a parameter that is a string, so it should be char* check_str(char *)

    Code:
    check_str(char string2)
    {
    char string2[40]; /*you don't need this*/
    As your function gets string2 as parameter, you don't need to redeclare it inside the function.

    Code:
    char string3[40];
    string3[4]={'a','b','c','\0'};
    char string3[40] means a char array of 40 elements; string3[4] is the 5th element in the array, it can be a single char...
    If you want to define a string, it should be more like
    Code:
    char string3[]="abc";
    And you don't need to include <conio.h>

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void check_str(char);
    1. you try and use the result (so returning void isn't good)
    2. you pass an array, so the parameter should be char*

    > void main
    You know what to do
    <---- look at this avatar!

    > gets(string1);
    This is also in the FAQ as a bad thing to do as well.
    gets() offers NO protection against buffer overflow.

    > checkstr
    Code:
    check_str(char string2)
    {
    char string2[40];
    char string3[40];
    string3[4]={'a','b','c','\0'};
    
    
        strcat(string2,string3);
       return char string2[40];
    }
    I'm thinking you're trying to append something to a string right?
    Code:
    char *check_str(char *string2)
    {
        char string3[]= "abc";
        strcat(string2,string3);
        return string2;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hi frrossk

    Thanks for your reply,

    I have tried what you suggested and get this error, can you think what this is?

    The error message says:
    Function should return a value in function main()

    Thanks again
    C

    The code is now :

    Code:
    #include<stdio.h>
    #include<string.h>
    #include <ctype.h>
    
    /*Functions Prototypes */
    char* check_str(char *);
    
    
    /*The main program*/
    
    int main()
    {
    char string1[40];
    char answer[40];
    
    
        printf("\n\n\t\t   Enter string1\n\n");
        printf("Please enter a string \n");
        gets(string1);
            
        strcpy(answer,check_str(string1));
        printf("answer = %s",answer);
    
    }  /* ERRORS OUT HERE*/
    
    
    /*Function To Concatonate String Passed*/
    char *check_str(char *string2)
    {
    char string3[]="abc";
    
    
    /*Concatenate string3 to string2 and return string2. */
    
    strcat(string2,string3);
    return char string2[40];
    }
    Last edited by colinuk; 01-31-2005 at 08:02 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    I got it

    Got it guys,

    Thanks a bunch.

    I changed the return in my function to

    Code:
    return string2;
    I realy need to figure out how to declare strings properly I was realy jumbled up there.

    Thanks again
    C

  6. #6
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    In function char *check_str(char *string2):
    Code:
    char *check_str(char *string2)
    {
    char string3[]="abc";
    
    strcat(string2,string3);
    return char string2[40];
    }
    don't return char string2[40], just return string2; - the variable has been already declared; you have to specify only the name of the variable you want to return.

    And to the end of int main() add return 0;.
    -------------------------------------
    A little late, I see...

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    Just a last question

    Hey Guys,

    Just an after thought, Its nice having the program run but im not too sure what the new bits I add have realy done.

    Where I have added char * what is that actually saying or doing, does this just mean the char is a string or is the * into pointers??

    I looked through the C ref guide at:

    http://www.infosys.utas.edu.au/info/....html#Contents

    But never came up with much.

    Thanks again
    C

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Where I have added char * what is that actually saying or doing, does this just mean the char is a string or is the * into pointers
    char *<variable> is read from right to left as variable is a pointer to a type char. In C, strings are simply char arrays. So basically what your doing is passing the address of the first char of the array.

    Additionally, since you didn't modify the value passed to the function. You could redefine the function to be of type Void, since you really didn't change the address of the char array (only the contents of the array) and therefore there's no need to return a value back.
    Last edited by Scribbler; 01-31-2005 at 09:41 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Scribbler
    char *<variable> is read from right to left as variable is a pointer to a type char. In C, strings are simply char arrays. So basically what your doing is passing the address of the first char of the array.
    No. They aren't "simply char arrays". They are similar to character arrays, but there is more to it. The following is not a string:
    Code:
    char foo[3] = { 'b', 'a', 'r' };
    Strings are null terminated. Arrays do not have to be null terminated. For that matter, a pointer to a malloced block of memory of type char does not have to have a null.

    The definition of a string is a null terminated group of characters.

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

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by Scribbler
    char *<variable> is read from right to left as variable is a pointer to a type char. In C, strings are simply char arrays. So basically what your doing is passing the address of the first char of the array.

    Additionally, since you didn't modify the value passed to the function. You could redefine the function to be of type Void, since you really didn't change the address of the char array (only the contents of the array) and therefore there's no need to return a value back.
    Hey scribbler

    << where you say "So basically what your doing is passing the address of the first char of the array."

    That is where the code says

    Code:
    char *check_reg(char *reg)
    in the (char *reg) of the code yea?

    And when this is ran it moves to the next address untill it hits an address that holds a null terminator??


    What about the code that says:

    Code:
    /*Functions Prototypes*/
    char * check_reg(char*);
    What am I saying here is this still read right to left?


    Thanks again
    C

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    That is the same thing, it is the function prototype for the function you declared above, aka it lets the compiler know that you have a function with a return type of a pointer to a char that takes a pointer to a char as an argument.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM