0

I am still new to pointers in C and have been trying to make string functions. This one is supposed to concatenate two strings, but when I try to run it, it crashes.

void concatenate (char *a, char *b, int size_a, int size_b) {
    char *c, *d, *e;
    c = a;
    d = b;
    int i, j, k;
    int l = 1;

    for(i = 0; i<size_a; i++) {
        *(e+i) = *(c+i);
    }
    for (j = size_a; j<size_a+size_b; j++) {
        *(e+j) = *(d+(j-(j-l)));
        l++;
    }
    for (k = 0; k< size_a+size_b; k++){
        printf("%c",*(e+k));
    }
}

What am I doing wrong?

7
  • 1
    Crashes your compiler? Really? Can you show the error message? Commented Feb 19, 2013 at 19:21
  • 1
    Not only that your function takes char *a and char *b as parameters (meaningless names), but its body even starts with char *c, *d, *e; c = a; d = b; Commented Feb 19, 2013 at 19:21
  • 5
    Hint - e doesn't point at anything. Commented Feb 19, 2013 at 19:22
  • Well I would like e to point to the new string. How would I go about doing this? Commented Feb 19, 2013 at 19:24
  • 1
    ... allocate some memory for it? Commented Feb 19, 2013 at 19:25

3 Answers 3

2

You are trying to access the memory where uninitialized pointer e points to, which leads to undefined behavior.

Your function should look like this:

char* concatenate (const char *a, const char *b, int size_a, int size_b) {
    // create new string:
    char* newStr = malloc (size_a + size_b + 1);
    int i, j;

    // copy first string into it:
    for (i = 0; i<size_a; i++)
        newStr[i] = a[i];

    // copy second string into it:
    for (j = 0; j < size_b; j++)
        newStr[i + j] = b[j];

    newStr[i + j] = '\0';
    return newStr;
}

Note that function doesn't change strings that are passed to it, thus they can be passed as const. The size_a and size_b are expected to be equal to strlen(a) and strlen(b), which also means that in case you always pass null-terminated strings into this function, you can calculate their length on your own within its body (so you can get rid of last 2 arguments).

Sign up to request clarification or add additional context in comments.

4 Comments

How do I get it so "concatenate" returns a new string?
Shouldn't the count of size_a and size_b include the null terminator. Then the malloc size parameter should be set to (size_a + size_b - 1).
@TallSoftwareDude: That's explained in the lines under the code.
@user2041197: You're welcome :) Also don't forget to call free on the pointer returned from concatenate function. You should free the memory that has been allocated by malloc once you don't need this memory anymore.
0

e is an uninitialized pointer, which means it is pointing to some random place in memory. When you start writing to it as if it were a pointer to a character array, your program will crash (or worse, change some state mysteriously causing unspecified behavior later on).

Comments

0

Your code seems obfuscated for no reason. The problem is that you're assigning to e, which is uninitialized. The following is, pretty much, your code in a more readable form and with e allocated. I haven't tested it, though.

void concatenate (char *c, char *d) {    
    char *e; 
    e = calloc(strlen(c) + strlen(d) + 1, sizeof(char));
    int i, j;
    int l = 1;

    for(i = 0; c[i]; i++) {
        e[i] = c[i];
    }
    l = i;
    for (j = 0; d[j]; j++) {
        e[l + j] = d[j];
    }
    e[l + j] = '\0';

    printf("%s", e);

    free(e);
}

1 Comment

the original passes in the string sizes. it is the norm, but how do you know that the strings are null terminated?

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.