First of all, when you do:
num = "123056";
You are not copying the string "123056" to the area of heap allocated by malloc()
. In C, assigning a char *
pointer a string literal value is equivalent to setting it as a constant - i.e. identical to:
char str[] = "123056";
So, what you've just accomplished there is you've abandoned your sole reference to the 100-byte heap area allocated by malloc()
, which is why your subsequent code doesn't print the correct value; 'p
' still points to the area of heap allocated by malloc()
(since num
pointed to it at the time of assignment), but num
no longer does.
I assume that you actually intended to do was to copy the string "123056" into that heap area. Here's how to do that:
strcpy(num, "123056");
Although, this is better practice for a variety of reasons:
strncpy(num, "123056", 100 - 1); /* leave room for \0 (null) terminator */
If you had just done:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *num = malloc(100);
char *p = num;
strncpy(num, "123056", 100 - 1);
p = p + 3;
*p = '4';
printf("%s\n", num);
return 0;
}
You would have gotten the correct result:
123456
You can contract this operation:
p = p + 3;
*p = '4';
... and avoid iterating the pointer, by deferencing as follows:
*(p + 3) = '4';
A few other notes:
Although common stylistic practice, casting the return value of malloc()
to (char *)
is unnecessary. Conversion and alignment of the void *
type is guaranteed by the C language.
ALWAYS check the return value of malloc()
. It will be NULL if the heap allocation failed (i.e. you're out of memory), and at that point your program should exit.
Depending on the implementation, the area of memory allocated by malloc()
may contain stale garbage in certain situations. It is always a good idea to zero it out after allocation:
memset(num, 0, 100);
Never forget to free()
your heap! In this case, the program will exit and the OS will clean up your garbage, but if you don't get into the habit, you will have memory leaks in no time.
So, here's the "best practice" version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *num, *p;
/*
* Don't take 1-byte chars for granted - good habit to get into.
*/
num = malloc(sizeof(char) * 100);
if(num == NULL)
exit(1);
memset(num, 0, sizeof(char) * 100);
p = num;
strncpy(num, "123056", 100 - 1);
*(p + 3) = '4';
printf("%s\n", num);
free(num);
return 0;
}
The solution is simple, declare your string in the following way instead
char str[] = "string";
The reason why you should do this is because of the Undefined behavior. Creating a string with pointers will make your string locate at the read only
memory part, so you cannot modify it, whereas another way will also make a copy of your string on the stack. Also check What is the difference between char s[] and char *s in C?
Best Answer
One problem lies with the parameter you pass to the function:
This is a static string allocated in the read-only portion. When you try to overwrite it with
you'll get the segfault.
Try with
and you should notice a difference.
The key point is that in the first case the string exists in the read-only segment and just a pointer to it is used while in the second case an array of chars with the proper size is reserved on the stack and the static string (which always exists) is copied into it. After that you're free to modify the content of the array.