Possible Duplicate:
Why does simple C code receive segmentation fault?
Hey Everyone, I'm sure this is a very basic question, but apparently I'm not quite understanding something here.
I've been playing around with C a lot over winter break and just came across something that I thought would work but is giving me a segmentation fault.
if I declare a string as: char name[5] = "Mike"; I can manipulate the string: *(name+1) = 'a'; This works fine, name becomes "Make".
If I declare as: char *name = "Mike"; and then try the same thing: *(name+1) = 'a'; I get a segmentation fault. Why can't I do that?
If I malloc the space for the string: char *name = (char*)malloc(5*sizeof(char)); and then copy the string to name: strcpy(name,"Mike"); I can manipulate it like above just fine. *(name+1) = 'a'; works.
What is the difference between char *name = "Mike"', and char *name = (char*)malloc(5*sizeof(char)); strcpy(name,"Mike");? Aren't they both just pointing to memory containing the string?
Sorry for the noobish question!