Reason for your error
You are trying to modify a string literal, which is a non-modifiable object.
That's why you get a segmentation fault.
Even if you simply call ptr[0]++
, it will be a segmentation fault.
Solution
One solution is to change the declaration to:
char ptr[] = "C programming"
Then you can modify the char array.
They look similar? Yes, the string literal is still non-modifiable, but you have declared an array with its own space, which will be initialized by the string literal, and the array is stored in the stack, and thus modifiable.
Example
Here is a full code example:
#include <stdio.h>
int test() {
char str[]="C programming";
char *ptr = str;
while(*ptr != '\0') {
// print original char,
printf("%c \n", *(ptr++));
// print original char plus 1, but don't change array,
// printf("%c \n", (*(ptr++))+1);
// modify char of array to plus 1 first, then print,
// printf("%c \n", ++(*(ptr++)));
}
return 0;
}
int main(int argc, char * argv[]) {
test();
return 0;
}
Tip: you should only enable one of the printf()
lines at the same time, due to ++
operator.
More tips
Note that we declared a ptr
and a str
, because we can't use ++
operation on an array (you can't change the address of an array), thus ++str
will get a compile error, while ++ptr
won't.
@Update - About memory
(To answer your comment)
- A string literal is usually stored in a read-only area of a process; refer to C string literals: Where do they go?
- A char array, if declared inside a method, then it's allocated on the stack, thus you can modify it; when you initialize a char array from a string literal, there are actually 2 copies of the same value: 1 is read-only; 1 is modifiable; the char array is initialized from the read-only copy.
- A char pointer it stores a single address; the pointer itself is allocated on stack in this case, and you can modify it.
You might also want to know more about pointer or address or array or Linux process memory layout_ or data sections of a C program; try searching on Google, or refer to books like The C Programming Language, 2nd Edn and The Linux Programming Interface — though this is a question about C rather than Linux. There's also The Definitive C Book Guide and List on Stack Overflow.