CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2008
    Posts
    5

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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';
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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

  4. #4
    Join Date
    Jan 2008
    Posts
    5

    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
    --------
    G++ Programmerak(=little programmer)
    QT App Developer
    Persian Programming Tutorials :
    http://CodersKiosk.Blogspot.Com

  5. #5
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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.

    Quote 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

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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"?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jan 2008
    Posts
    5

    Re: char pointer question

    Quote 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!

    Quote 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!
    --------
    G++ Programmerak(=little programmer)
    QT App Developer
    Persian Programming Tutorials :
    http://CodersKiosk.Blogspot.Com

  8. #8
    Join Date
    Oct 2004
    Posts
    296

    Re: char pointer question

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured