Click to See Complete Forum and Search --> : char pointer question
sepehr
March 16th, 2008, 10:44 AM
please tell WHY this propagates error?(segmentation fault)
char *a;
a="i'm A string!";
*a='I';//this line is logically wrong
I'm trying to change first character with the code(just an example)
---
thanks in advance
laserlight
March 16th, 2008, 10:48 AM
Since a points to a string literal, and you are trying to change a char using a, you are trying to change a char literal. What you should do is along the lines of:
char a[14];
strcpy(a, "i'm A string!");
*a = 'I'; // or a[0] = 'I';
Hermit
March 16th, 2008, 11:01 AM
To avoid this kind of runtime error, only assign string literals to const char *s (assuming a narrow character):
const char *a = "i'm A string!";
*a='I'; //this line is logically wrong, and fortunately doesn't compile
If you need to modify the string, either do as laserlight has shown, or use a std::string.
sepehr
March 16th, 2008, 11:02 AM
Since a points to a string literal, and you are trying to change a char using a, you are trying to change a char literal.
would you share more light on it?
i knew the solution i wanted to know more about that error
Hermit
March 16th, 2008, 11:07 AM
To avoid this kind of runtime error, only assign string literals to const char *s (assuming a narrow character):
const char * a = "i'm A string!";
*a='I'; //this line is logically wrong, and fortunately doesn't compile
If you need to modify the string, either do as laserlight has shown, or use a std::string.
would you share more light on it?
i knew the solution i wanted to know more about that error
String literals exist in a special segment of the executable that may not be modified at runtime. When you assign a string literal to a pointer, you are then pointing to that static memory, so dereferencing the pointer and trying to modify the string is an error.
laserlight
March 16th, 2008, 11:09 AM
would you share more light on it?
i knew the solution i wanted to know more about that error
Do you understand what I mean by "a points to a string literal"?
sepehr
March 16th, 2008, 11:15 AM
String literals exist in a special segment of the executable that may not be modified at runtime. When you assign a string literal to a pointer, you are then pointing to that static memory, so dereferencing the pointer and trying to modify the string is an error.
aha!that was what i wanted to know thanks alot hermit!
Do you understand what I mean by "a points to a string literal"?
you DON'T have to repeat ! :P what i asked was clear!
7stud
March 16th, 2008, 09:45 PM
aha!that was what i wanted to know thanks alot hermit!
To put it more simply, string literals are const. A string literal is anything between double quotes.
A little more detail: C++ takes a string literal, slaps '\0' character on the end of it, and then transforms it into a char array that cannot be changed, in other words the string literal becomes a const char array. Try this:
int* p;
p = "hello";
You should get an error that says something like:
error: cannot convert const char[6] to int*
That shows that the string literal is first converted to a char array before anything else happens.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.