Thread: please help with function returning string

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    138

    please help with function returning string

    Code:
    #include <strings.h>
    #include <stdio.h>
    
    char glew(char a[], char b[]);
    
    int main(void)
    {
        char a[10] = "apple & ";
        char b[10] = "pear";
        
        printf("%s",glew(a,b));
        return 0;
    }
        
    char glew(char a[], char b[])
    {
        char c[40];
        c = strcat(a,b);
        return c;
    }
    Could someone please correct the lines in the function?
    I have tried hard myself. Cannot do it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,415
    Did you get any of these errors?
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:11:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
         printf("%s",glew(a,b));
                ^
    foo.c: In function ‘glew’:
    foo.c:18:9: warning: implicit declaration of function ‘strcat’ [-Wimplicit-function-declaration]
         c = strcat(a,b);
             ^
    foo.c:18:9: warning: incompatible implicit declaration of built-in function ‘strcat’
    foo.c:18:9: note: include ‘<string.h>’ or provide a declaration of ‘strcat’
    foo.c:18:7: error: assignment to expression with array type
         c = strcat(a,b);
           ^
    foo.c:19:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
         return c;
                ^
    foo.c:19:12: warning: function returns address of local variable [-Wreturn-local-addr]

    Like this.
    Code:
    char* glew(char a[], char b[])
    {
        static char c[40];  // So it's value is preserved on function return
        strcpy(c,a);
        strcat(c,b);
        return c;
    }
    Or like this, where the result array is provided by the caller.
    Code:
    char* glew(char a[], char b[], char c[])
    {
        strcpy(c,a);
        strcat(c,b);
        return c;
    }
    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.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Thank you. (re line 1 I had searched the Net and had found a char* solution, but I also found that char* is not (/ no longer) allowed.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,415
    > but I also found that char* is not (/ no longer) allowed.
    What are you smoking?
    You need to cite the reference for that ridiculous assertion.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Quote Originally Posted by Salem View Post
    > but I also found that char* is not (/ no longer) allowed.
    What are you smoking?
    You need to cite the reference for that ridiculous assertion.
    When I search the Net for char* being disallowed in a C function definition, I cannot find such information. So I am puzzled.

    Thank you for your clear response to my query.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a string in a function
    By rpmischris in forum C Programming
    Replies: 2
    Last Post: 10-09-2012, 09:54 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. returning a string from a function
    By budala in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 10:03 PM
  4. returning a string from a function
    By revelation437 in forum C Programming
    Replies: 6
    Last Post: 12-14-2002, 04:21 AM
  5. returning a string from a function
    By itld in forum Linux Programming
    Replies: 5
    Last Post: 12-03-2001, 01:35 AM

Tags for this Thread