im trying to change a part of a string using another pointer.
what I have
char** string = (char**) malloc (sizeof(char*));
*string = (char*) malloc (100);
*string = "trololol";
char* stringP = *string;
stringP += 3;
stringP = "ABC";
printf("original string : %s\n\n", *string);
printf("stringP : %s\n\n", stringP);
What I get
original string : trololol;
stringP : ABC;
what I whant is troABCol in both of them 😀
I know I have a pointer to a string (char**) because thats what I need in order to do this operation inside a method.
you need to do
strcpy(*string, "trololol")instead of*string = "trololol";. Your solution brings memory leak, as it replaces the memory pointer allocated bymalloc()with pointer to data, which contains the pre-allocated “trololol” string.strcpy()copies the pure string pointed to, and instead ofstringP = "ABC";, you can domemcpy(stringP, "ABC", 3)(strcpyappends\0at the end, whereasmemcpycopies only data it is told to copy).