0

Which are wrong()?

(a)
const char * p = "apple"; 
p = "pig";

(b)
const char * p = "apple"; 
p[1] = 'x';

(c)
char * const p = "apple";
p = "pig";

(d)
char * const p = "apple";
p[1] = 'x';

I thing (b) and (c) are wrong.

(a) p: can change、const char: can't change.

(b) p: can change、const char: can't change.

(c) const p: can't change、char: can change.

(d) const p: can't change、char: can change.

(a)
const char * p = "apple";    (O)
p = "pig";                // change pointer
    
(b)
const char * p = "apple";    (X)
p[1] = 'x';               // change value
    
(c)
char * const p = "apple";    (X)
p = "pig";                // change pointer
    
(d)
char * const p = "apple";    (O)
p[1] = 'x';               // change value

but when I using online complier to answer (d), I got the "Segmentation fault". I can't figure out what's going on. enter image description here

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
陳政逸
  • 11
  • 2
  • Removed the c tag as it's a c++ question. – Allan Wind Aug 14 '22 at 05:20
  • 2
    C++ should be learnt using a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of by solving random online puzzles. – Jason Aug 14 '22 at 06:22

0 Answers0