0

I'm a little perplexed why the following blows up:

char* c = "Hello World!";
*c = 'h';

When I allocate the string on the heap it works. So I'm just curious what wrong with my initial version.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
chhenning
  • 2,017
  • 3
  • 26
  • 44
  • On a modern C++ compiler this won’t even compile, or at the very least will issue a stern warning. – Konrad Rudolph Apr 04 '13 at 14:25
  • @KonradRudolph do you mean that this `char * c = "ccxcv"`; acts like a `char * const c;` ? – Stephane Rolland Apr 04 '13 at 14:27
  • @Stephane No, as `char const*`, “warning: conversion from string literal to 'char *' is deprecated”. – Konrad Rudolph Apr 04 '13 at 14:27
  • Quote on the question used as original: "*This question gets asked about once a week on SO :), give me a minute to find the original and I'll link you to it.*". **Still relevant** – UmNyobe Apr 04 '13 at 14:28
  • The [c-faq.com](http://c-faq.com/) website answers this question and more... which leads me to believe you either haven't bothered to research, or aren't very good at it. Please ensure you read the *entire site* as soon as possible, to answer any future questions you have *before* problems like this arise. – autistic Apr 04 '13 at 14:28
  • @KonradRudolph , it's clearer, thx. – Stephane Rolland Apr 04 '13 at 14:28

5 Answers5

6

char* c = "Hello World!"; is a pointer to a string literal which is typically stored in a read-only memory segment. Attempting to modify it is undefined behaviour. Pointers to string literals such as this should more properly be defined as

const char *c = "Hello World!";

but the const is often omitted (in C, at least).

Paul R
  • 208,748
  • 37
  • 389
  • 560
3
char* c = "Hello World!";

Here c is a pointer which point to a literal string so you can not modify it

You can use this instead

char c[] = "Hello World!";
*c = 'h',

c here is an array of char and contains the chars of the string "Hello World!" so you can modify it.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
3

You're pointing c to a string literal, most likely stored in the read only memory segment, you can't change it. Even if you can physically change it, as per the C spec:

6.4.5 (String Literals)

If the program attempts to modify [a string literal], the behavior is undefined.

If you allocate memory on the heap (or stack) and then copy the string to that location, you can change it as you see fit.

Mike
  • 47,263
  • 29
  • 113
  • 177
2

Modifying string literals is undefined behaviour. The main reason for that is that the compiler is permitted to place "Hello World!" in read-only memory.

On the other hand, the following is fine:

char c[] = "Hello World!";
*c = 'h';
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • It might be useful to explain to the questioner how it is that they have attempted to modify a string literal, rather than merely stating it is undefined behavior. – Eric Postpischil Apr 04 '13 at 14:31
-1

Strings like char * c = "Hello"; are string constants and are stored in read only data segment so you can't modify them [But some compilers do allow that]

Heap allocated strings are not in read only segments and are hence free to be modified.

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • I did not down vote but your answer is wrong. because a `char *p;` pointer could be pointed to a char array `char arr[30];` Update uor answer otherwise there is risk to get more downvoted – MOHAMED Apr 04 '13 at 14:40
  • Thanks @Mohamed . I hope the people who downvote would add the reason too – Suvarna Pattayil Apr 04 '13 at 14:42
  • It's the first time I know that there is some compiler allow that could you please provide some references – MOHAMED Apr 04 '13 at 15:23
  • Your capitalization suggests you're talking about ROM chips. Notice Paul R uses "read-only memory segment" which is different. It's all in RAM. Since modifying string literals is *undefined behavior* per the standard, compilers *aren't allowed* to allow it; at least, not in standards-compliance mode. – luser droog Apr 09 '13 at 07:38
  • Actually I just wanted to say "read only area". Thanks, for pointing that out. – Suvarna Pattayil Apr 09 '13 at 07:43