Difference between the following declarations and definitions

68 Views Asked by At

According to my textbook,

  1. char *p = "hello";

    • A pointer to a string where both the string and the pointer can be modified
  2. const char *p = "hello";

    • A pointer to a constant string where the string cannot be manipulated but the pointer can be manipulated to point to another char string
  3. char *const p = "hello";

  • A constant pointer to a string where the string can be manipulated but the pointer cannot be manipulated to point to another char string
  1. const char *const p = "hello";
  • both the string or the pointer cannot be changed.

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.enter image description hereenter image description here

2

There are 2 best solutions below

5
Vlad from Moscow On BEST ANSWER

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  
0
gulpr On

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;
}

No, it is Undefined Behaviour as string literals cannot be modified. So, this example is invalid.

Your book is completely wrong and should not be used to learn C. All the examples show the author's lack of C knowledge.

  1. You can't modify string literals (ie "fgfdfdgfgfdg")
  2. You cant modify const objects.
  3. If you try to modify const object via pointer to not const object you invoke Undefined Bahavior and your program is not valid anymore.