2

Possible Duplicate:
Modifying value of char pointer in c produces segfault

This is a piece of code ...

void main()
{
    char *p="Hello";
    *p= 'h';                      // Segmentation fault .
}

I understand the fact that there is a segmentation fault and it gives me a run time error also .But I wonder , why is it a RUN TIME ERROR ?? Why cant the compiler tell me before executing the program ? Why does not it show a COMPILE TIME ERROR ?

PS : I use Visual C++ 2005 Express ..

Community
  • 1
  • 1
progammer
  • 1,951
  • 11
  • 28
  • 50
  • 2
    Asked many times before, e.g., http://stackoverflow.com/questions/5189782/modifying-value-of-char-pointer-in-c-produces-segfault – Tom Zych Sep 02 '11 at 06:35
  • Have you enabled compiler warnings? I don't know about Visual C++, but GCC (with the default warning level) certainly gives a warning for the conversion from string constant to `char *`. It also correctly gives an error for not returning `int` from main. – Mike Seymour Sep 02 '11 at 06:41
  • @ Mike .. I am not sure .. The problem is I have used Turbo C to learn C .. (please dont laugh :D) .. and am new to visual C++ compiler .. I dont know how to enable compiler warnings and I also think that by default it is set 'disabled' .. as I dont get a warning even when I dont 'return' from int main() . – progammer Sep 02 '11 at 07:20
  • @Appy: In C++, no return from `main` is necessary; if your code falls off the bottom, the compiler effectively inserts `return 0;` automatically. But it must still have a return type of `int`. – C. K. Young Sep 02 '11 at 07:34
  • @ Chris .. THanks for ur reply .. :) I also found this useful .. I hope you meant to convey this .. http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c But my program also runs with void main() .. ?!! – progammer Sep 02 '11 at 07:47

3 Answers3

2

String literals are really of type char const*. However, for compatibility with older C code that's not const-correct, C++ allows them to be assigned to a char*. That does not mean you are really allowed to modify them.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 1
    +1 they are read-only. Use `char p[] = "Hello";` and it would work, though. –  Sep 02 '11 at 06:20
  • Thanks for the answer. But I still got an issue . What do you mean by "compatibility with older C code thats not const-correct " ? :? I am trying to understand it , but it's not clear . While searching for it's meaning, I came across a link which explains it exactly .. !! http://www.possibility.com/Cpp/const.html – progammer Sep 02 '11 at 07:38
  • @Appy: C++ introduced the concept of const and const-correctness; prior to C++, the concept of const didn't exist. Strictly speaking, for const-correct code, you should never be able to assign a string literal to `char*`. However, because a lot of people migrate code from C to C++, some concessions had to be made to allow the migration to be smoother. This is one such concession. – C. K. Young Sep 02 '11 at 07:41
  • "This appears to be an inconsistency in the language standard. A lot of these inconsistencies exist because older C and C++ code would break if the standard were strictly consistent. The standards people are afraid to break old code, because it would mean a decrease in the popularity of the language." Hoping that this is the reason for it not to be shown as a COMPILER ERROR , can u temme how will it make the language less popular .. Strange ?? :? – progammer Sep 02 '11 at 07:51
  • @Chris .... Yeah ... I got your point .. THanks a loot .. :) – progammer Sep 02 '11 at 07:53
  • @Chris ... But somehow , rather than calling this thing a concession .. I somehow feel , this is a flaw .. Because runtime errors can be harmful to ur PC ... is nt it ? – progammer Sep 02 '11 at 08:00
0

Your fault cannot manifest itself at compile time; there, both your statements are completely valid. It is at runtime when the string "Hello" is read-only and you're trying to modify it.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
0
char *p="Hello";

type of expressions are considered deprecated. "Hello" is a string literal stored in a read only memory area; attempting to modify those locations is an Undefined Behavior. In good platforms it results in a crash / segmentation fault

They are expressed as,

const char *p = "Hello";

which means that p is not allowed to be modified. If you want to let p be modifiable then declare it as,

char p[] = "Hello";  // 'p' is an array of size 6
iammilind
  • 68,093
  • 33
  • 169
  • 336