|
-
March 16th, 2008, 10:44 AM
#1
char pointer question
please tell WHY this propagates error?(segmentation fault)
Code:
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
-
March 16th, 2008, 10:48 AM
#2
Re: char pointer question
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:
Code:
char a[14];
strcpy(a, "i'm A string!");
*a = 'I'; // or a[0] = 'I';
-
March 16th, 2008, 11:01 AM
#3
Re: char pointer question
To avoid this kind of runtime error, only assign string literals to const char *s (assuming a narrow character):
Code:
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.
- Alon
-
March 16th, 2008, 11:02 AM
#4
Re: char pointer question
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
-
March 16th, 2008, 11:07 AM
#5
Re: char pointer question
To avoid this kind of runtime error, only assign string literals to const char *s (assuming a narrow character):
Code:
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.
 Originally Posted by sepehr
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.
- Alon
-
March 16th, 2008, 11:09 AM
#6
Re: char pointer question
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"?
-
March 16th, 2008, 11:15 AM
#7
Re: char pointer question
 Originally Posted by Hermit
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!
 Originally Posted by 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!
-
March 16th, 2008, 09:45 PM
#8
Re: char pointer question
 Originally Posted by sepehr
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:
Code:
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.
Last edited by 7stud; March 16th, 2008 at 09:56 PM.
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
|