Why is my code failing to copy one string to another?

1

I want to write a C function called append() the target of which would be to append a character at the end of a given string. But, I am having trouble to get my code work because of some pointer-related issue.

#include <stdio.h>

void append(char ** string, int length, char ch)
{
    printf("%s", string);

    char * newString = (char *) malloc(sizeof(char)*(length+2));

    strcpy(newString, string);

    newString[length ] = ch;
    newString[length + 1] = '\0';

    strcpy(string, newString);

    free(newString);
}

int main()
{
    char * string = "my1name234is56";
    append(string, strlen(string), 'x');

    printf("%s", string);
}

Inside the function append(): the second strcpy() before free() is giving me an exception, and the program is showing:

Unhandled exception at 0x5E8D40D9 (msvcr120d.dll) in mips_2_1.exe: 
0xC0000005: Access violation writing location 0x00D1585C.

I have also tried

strcpy(*string, newString);

and

strcpy(**string, newString);

and, nothing seems to be working.

.

strcpy(&string, newString);

seems to be working, but, the end result is not what I would expect.

Edit: I made the edit according to alk.

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

void append(char ** string, int length, char ch)
{
    printf("%s\n\n", *string);

    char * newString = (char *)malloc(sizeof(char)*(length + 2));

    strcpy_s(newString, length+1, *string);

    newString[length] = ch;
    newString[length + 1] = '\0';

    strcpy_s(*string, length + 2, newString);

    *string = newString;
}

int main(void)
{
    char * string = "my1name234is56";
    append(&string, strlen(string), 'x');

    printf("%s", string);
}

its still not working.

Edit2: I made the following edit according to "Weather Vane":

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

char * append(const char * string, int length, char ch)
{
    char * newString = (char *)malloc(sizeof(char)*(length + 5));

    printf("%s\n\n", string);

    strcpy_s(newString, length+5, string);

    newString[length] = ch;
    newString[length + 1] = '\0';

    return newString;
}

int main(void)
{
    char * string = "my1name234is56";

    printf("%s\n\n", string);

    char * string2 = append(string, strlen(string), 'x');

    printf("%s", string2);
}

Now, it is working.

Edit3: the following modification, according to alk, worked for me:

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

void append(char ** string, int length, char ch)
{
    char * newString = (char *)malloc(sizeof(char)*(length + 2));

    strcpy_s(newString, length + 1, *string);

    newString[length] = ch;
    newString[length + 1] = '\0';

    *string = newString;
}

int main(void)
{
    char * string = "my1name234is56";

    printf("%s\n\n", string);

    append(&string, strlen(string), 'x');

    printf("%s\n\n", string);
}
c
string
pointers
visual-studio-2013
asked on Stack Overflow Mar 28, 2019 by user366312 • edited Mar 28, 2019 by user366312

2 Answers

2

char * string = "my1name234is56"; is a string literal. There are two reasons why you can't write the extended string back to it:

  • There isn't enough memory for the extra character.
  • It is read-only.

In any case, the function definition has too many stars, it should be

void append(char * string, int length, char ch)

Or even better

void append(const char * string, int length, char ch)

which indicates it should not be written to, and the whole idea is off-limits. You have to create a new string and return a pointer to that one

char *append(const char * string, int length, char ch) {
    // ...
    return newString;
}

int main() {
    //...
    string = append(string, strlen(string), 'x');
}
answered on Stack Overflow Mar 28, 2019 by Weather Vane • edited Mar 28, 2019 by Weather Vane
2

You are almost there.

Still, you need to change the value of the pointer you pass in to the value of the pointer to the memory you allocate instead of freeing it.

The below only shows the lines changed.

...

void append(char ** string, size_t length, char ch)
{
    printf("%s\n", *string);

    ...

    strcpy(newString, *string);

    ...

    *string = newString;
}

int main(void)
{
    ...
    append(&string, strlen(string), 'x');
    ...
}

You, BTW, do not need to pass in the size of the source, but you can easily derive it inside the function by doing strlen(*string).


Sry for the incomplete code. I posted the answer from sitting behind a highly complete code-leakage-detection system, it seems.

answered on Stack Overflow Mar 28, 2019 by alk • edited Mar 28, 2019 by alk

User contributions licensed under CC BY-SA 3.0