简体   繁体   中英

How to pass a String as a parameter and then return a String in C

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.

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);
}

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;

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM