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

void catTo(char *dest, const char *str1, const char *str2)
{
    dest = (char *) malloc(1 + strlen(str1)+ strlen(str2) );
    strcpy(dest, str1);
    strcat(dest, str2);
}

int main(int argc, char** argv) {
    char *str1 = "abcd";
    char *str2 = "defg";
    char *dest;
    catTo(dest, str1, str2);
    printf("%s",dest);

    return 0;
}

I have been trying to get a simple str copy function to work, however, when I print out dest, I get "(null)". I tried, messing around with putting &/* infront of certain variables, but to no prevail.

3
  • 2
    void catTo(char *dest, : dest won't be modified outside your function. Commented Feb 2, 2017 at 14:03
  • when compiling, always enable all the warnings, then fix those warnings. (for gcc, at a minimum use: -Wall -Wextra -pedantic I also use: -Wconversion -std=gnu99 ) Commented Feb 3, 2017 at 18:26
  • when calling any of the heap memory allocation functions (malloc, calloc, realloc) 1) do not cast the returned value. The type is already void* so can be assigned to any other pointer. Casting just clutters the code, making it more difficult to understand, debug, maintain. 2) always check (!=NULL) the returned value to assure the operation was successful Commented Feb 3, 2017 at 18:35

2 Answers 2

4

don't pass dest as an input parameter. Its start value is not needed (and undefined) and is not modified by the catTo function call, which explains your problem.

Better do this:

char *catTo(const char *str1, const char *str2)
{
    char *dest = malloc(1 + strlen(str1)+ strlen(str2) );
    strcpy(dest, str1);
    strcat(dest, str2);
    return dest;
}

and in the caller:

char *dest = catTo(str1, str2);
Sign up to request clarification or add additional context in comments.

1 Comment

Did you mean to write char dest = /*...*/in catTo?
4

I tried, messing around with putting &/* infront of certain variables, but to no prevail.

That's a terrible way of solving problems - you should review your knowledge on pointers and value semantics instead of randomly adding operators to your code.

Regardless, the issue is that you're passing dest as a char* and then assigning to dest inside catTo: since you're copying dest, you're assigning to the local argument, not to the dest instance present in main.

You can solve this issue by passing dest as a char**:

void catTo(char **dest, const char *str1, const char *str2)
{
    *dest = malloc(1 + strlen(str1)+ strlen(str2) );
    strcpy(*dest, str1);
    strcat(*dest, str2);
}

int main(int argc, char** argv) {
    char *str1 = "abcd";
    char *str2 = "defg";
    char *dest;
    catTo(&dest, str1, str2);
    printf("%s",dest);

    return 0;
}

Also, every malloc should be matched by a corresponding free. Don't forget to call free(dest) when you're done with it in main:

int main(int argc, char** argv) {
    char *str1 = "abcd";
    char *str2 = "defg";
    char *dest;
    catTo(&dest, str1, str2);
    printf("%s",dest);
    free(dest);

    return 0;
}

2 Comments

no need for that char* cast in malloc: (char *) malloc => malloc
@Jean-FrançoisFabre still not Javascript's level of lenience, but pretty close!

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.