|
-
February 25th, 2012, 11:11 PM
#6
Re: Segmentation fault and also bad initalization
 Originally Posted by jedipenguin
Ok, so I set the value of s just to see what happens. Still getting the segmentation fault.
That is because "s" is a string-literal, and you cannot write to string literals.
Here is a much simpler program:
Code:
int main()
{
char *s = "abc123";
s[0] = 'x';
}
This program also crashes, and for the same reason your program crashes. The s is a string-literal, and I'm attempting to change the first character from 'a' to 'x'. Crash.
Only writeable char arrays, and memory allocated from the heap can be modified.
Code:
int main()
{
char s[] = "abc123";
s[0] = 'x';
}
This code now doesn't crash, since s is a writeable array of char.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|