-2

Possible Duplicate:
Why is this C code causing a segmentation fault?

char array[6] = "kapil";  
array [0] =  'K';    

this code runs ok.
but following code produce segmentation fault ?

char* array = "kapil";  
array [0] =  'K';   

why first does not produce seg-fault.

Community
  • 1
  • 1

2 Answers2

3

char* array = "kapil"; declares a string literal and is equivalent to const char*. This may well exist in read-only memory and cannot be modified.

Strictly speaking, attempts to modify it result in undefined behaviour but in practice a seg fault is likely.

simonc
  • 41,632
  • 12
  • 85
  • 103
1

char* array = "kapil"; using this type of declaration, it copies kapil in read-only context, you can change its contents.

Adeel Ahmed
  • 1,591
  • 8
  • 10