Question

Hello I am trying to copy a char * pointer to a char [] array.

this is my code so far

char * string_add(char * base, char * toAdd)
{
    char * string=malloc(strlen(base)+streln(toAdd)+1);
    sprintf(string,"%s%s",base,toAdd);
    char returnString[strlen(string)+1]; 
    // here comes my problem:
    memcpy(returnString,string,strlen(string)+1);
    // want to add free(string) here
    return returnString;
}

I want to use such a function to save code. I don't want to look after every allocated memory. I also tried

memcpy(&returnString,string,strlen(string)+1);

and some variants with strcpy and strncpy. But following problem persists:

if I call the function twice like:

int main(int argc, char * argv[])
{
    char * str1=string_add(argv[1],"-test1"); 
    char * str2=string_add(argv[1],"-test2"); 
    printf("%s, %s", str1,str2);
}

the output is like:

abc-test2, abc-test2

How can I realize this?

Thank you in advance!

Was it helpful?

Solution

in C, you have to look after the malloced memory, the char array you are declaring is on the stack, and will be gone after the function returns, only the malloc memory will hang around. and Yes, you will have to look after it and clean it up.

OTHER TIPS

I assume that the second call to your function overwrites the contents of the buffer created by the first call.

To my understanding, you are trying to concatenate two character strings.

Have a look in this answer.

Basically, since the length of the strings is not be known at compile-time, you'll need to allocate dynamic memory, while not forgetting to free it after you are done handling the new string.

Basically, storage that is NOT malloc'ed does NOT persist after a routine ends. You might be able to use global vars, but that would complexify a simple task, i.e. concatenate two strings.

char * string_add(char * base, char * toAdd)
{
    char *string_ptr = malloc(strlen(base)+streln(toAdd)+1);

    sprintf(string_ptr, "%s%s", base, toAdd);

    // string_ptr points to heap storage that is permanent
    return string_ptr;
}

// in main() ....

char *s1 = string_add("A", "B");
// s1 points to storage on the heap as: "AB\0"
//
free(s1);  // OR allow system to clean-up when main() exits

In the function

char * string_add(char * base, char * toAdd)
{
    char * string=malloc(strlen(base)+streln(toAdd)+1);
    sprintf(string,"%s%s",base,toAdd);
    char returnString[strlen(string)+1]; 
    // here comes my problem:
    memcpy(returnString,string,strlen(string)+1);
    // want to add free(string) here
    return returnString;
}

You define returnString[] but it only lives within the scope of the function. So you have to change the logic behind the use and the declaration of returnString[]

So there's a bit wrong with that code. First off you do:

char * string=malloc(strlen(base)+streln(toAdd)+1);

that is wrong, that assumes every char is 1 byte which it depends on the platform. What's better to do is:

char * string=malloc((strlen(base)+streln(toAdd)+1) * sizeof(char));

plus malloc is expensive in terms of CPU time and you don't free it. Doing what you're doing in that code doesn't need the malloc even. Just do this:

char returnString[strlen(base) + strlen(toAdd) + 1];

second of all, you should deal with the strings differently. What you can do is this:

strcpy(returnString,base);
strcat(returnString,toAdd);

or. The best thing to do for something like this is declare in your main str1 and str2, malloc them in main, pass the pointer to the pointer to the function. Have the function copy/cat the data like i showed above. and then finish. Now you don't need to deal with return types and all that inner mallocing and crap like that. You just deal with two mallocs in main and 2 free's in main after you use them. Here's the pseudo code:

int main(int argc, char *argv[])
{
    char * str1 = (char *) malloc((sizeof(argv[1]) + sizeof("-test1")) * sizeof(char));
    //do same for str2
    copyString(&str1,argv[1],"-test1");
    //do same for str2
    //print
    //free like this:
    free(str1);
    //do same for str2
    return 0;
}

now here's the copyString function:

void copyString(char ** returned, char * base, char * add)
{
    strcpy(returned,base);
    strcat(returned,add);
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top