According to my textbook,
char *p = "hello";
const char *p = "hello";
char *const p = "hello";
But here are my output for the different cases when the string can be modified -
#include <stdio.h>
int main() {
// Write C code here
char *p = "hello";
*p = 'a';
//p = "bye";
printf("%s",p);
return 0;
}
Output : Segmentation Fault
#include <stdio.h>
int main() {
// Write C code here
char *const p = "hello";
*p = 'a';
//p = "bye";
printf("%s",p);
return 0;
}
Output: Segmentation Fault
When do I get a segmentation fault in these cases when almost all the references I came across says otherwise? Thanks.
Though in C opposite to C++ string literals have types of non-constant character arrays nevertheless you may not change a string literal. Any attempt to change a string literal results in undefined behavior.
From the C17 Stndard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
Sp it is better in C to declare pointers to string literals with the qualifier const
as for example
const char *p = "hello";
If a pointer (without the qualifier const
) points to a non-constant array then you may change the array using the pointer. For example
char s[] = "hello";
char *p = s;
*p = 'a';
So it will be better and correct to rewrite all your examples using an intermediate array as shown above. For example
char s[] = "hello";
const char *p = s;
*p = 'a'; // compiler error
// trying to modify array through a pointer with the qualifier const
*s = 'a'; // correct; the array itself is not constant