0

I am trying to figure out how to pass a string as a parameter and then return another String in C. I am practicing by simply adding test. This is for a project I am stuck on where I need to do some validation on a string and wanted to do it in a seperate function. Obviously my example is more complicated than it should be but I need to know how to manipulate a string that has been passed as a parameter then return it as another string.

    #include <stdio.h>
#include <string.h>
#include <stdlib.h>

void stringCopy(char *); 

int main()
{
    char delay;
    char string[10];
    printf("Enter the string: ");
    scanf("%s", string);


    char newString[20];

    strcat(newString, stringCopy(string));   
    printf("String: %s", newString);

     delay = getchar();
     delay = getchar();
     }

/*char stringCopy(char *str)
{
     char string2[20] = "test";

     strcat(string2, str);

     return string2;

}*/

char* stringCopy(char *str)
{
     char *string2 = malloc(sizeof(char)*20);

     strcpy(string2,"test");
     strcat(string2, str);

     return string2;

} 

Here is all my code for my little practice program thanks in advance for any help.

**edited to show changes. I just don't know how to access the new new (string2) in the main().

edit2: I just can't seem to get it. I really appreciate everyones help though. Really frustrating. I'm going to keep trying but I don't even understand why i'm getting the errors or how to access my new string in main().

edit3: im an idiot, I had the file extension as .cpp and not as .c.

3
  • What's the problem here? Do you get wrong results? Commented Mar 29, 2014 at 15:32
  • I don't know how access the new string (string2) in the main() Commented Mar 29, 2014 at 15:46
  • Just save the function return in a char* variable. Then use it like any other string. Don't forget to free() it. Commented Mar 29, 2014 at 15:52

3 Answers 3

1

Your problem with stringCopy() is that string2 only lives inside the function. Once the final } is reached the object ceases to exist and any reference to it from other functions is erroneous.

You need to either pass a pointer for the result (1) or return a pointer to some object that will keep on living after the function terminates (2).

/* 1 UNTESTED */
void stringCopy(char *destin, size_t dlen, const char *source) {
    int n = snprintf(destin, dlen, "test%s", source);
    if (n >= dlen) /* error */;
}
int main(void) {
    char test[100];
    stringCopy(test, sizeof test, "foobar");
    printf("%s\n", test);
}

/* 2 UNTESTED */
char *stringCopy(const char *source) {
    int size;
    char *tmp;
    size = strlen(source) + 5;
    tmp = malloc(size);
    if (tmp) {
        int n = snprintf(tmp, size, "test%s", source);
        if (n != size - 1) /* error */;
    }
    return tmp;
}
int main(void) {
    char *test;
    test = stringCopy("foobar");
    printf("%s\n", test);
    free(test);
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are two common ways to deal with this kind of situation. You can allocate space on the heap to hold the newly generated string or your can pass an additional parameter to the function to tell it where to store the new string.

Allocating the new string on the heap

char *stringCopy(char* src) {
    char* dst = malloc(strlen(src) + 4 + 1);
    strcpy(dst, "test");
    strcat(dst, src);
    return dst;
}

int main() {
    char string[10];
    printf("Enter the string: ");
    scanf("%s", string);
    char* newString = stringCopy(string);
    printf("String: %s", newString);
    free(newString); // important!
}

Passing in a buffer as a parameter

void stringCopy(char* src, char* dst) {
    strcpy(dst, "test");
    strcat(dst, src);
}

int main() {
    char string[10];
    printf("Enter the string: ");
    scanf("%s", string);
    char newString[20];
    stringCopy(string, newString);
    printf("String: %s", newString);
}

Normally when passing a buffer, you would also pass in the buffer size so that the function can fail gracefully rather than overflow the buffer.

4 Comments

Could you explain char* dst = malloc(strlen(src) + 4 + 1); to me because it says "invalid conversion from 'void*' to `char*' " as an error. Thanks!
@user2976759 - It probably needs a cast.
The "error" invalid conversion from 'void*' to 'char*' is not an error in C. You need to use a C compiler for C code.
I'm an idiot i'm using c++ and not just C. This whole thing is probably completely wrong.
0

You need to return the string, not a character. Try this:

char* stringCopy(char *str)
{
     char *string2 = malloc(sizeof(char)*20);

     strcpy(strin2,"test");
     strcat(string2, str);

     return string2;

}

2 Comments

Also, see that Rikayan went for dynamic allocation. A stack-based array would be lost on function return... Though it does mean you have to free it in your main program.
Yeah his code solved one of my problems. It's accessing it in the main is where my next difficulty is. I should've made that clear in the original code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.