Thread: Very simple question on char * type

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Very simple question on char * type

    I get an error like this : Unhandled exception at 0x004115ae in LesPointeur2.exe: 0xC0000005: Access violation writing location 0x00417a28.

    I see that i cannot overwrite the "Hello World" like it is read only. "Hello World"
    is like a constant but I don't want to. I never declare char const *MyString so
    why I cannot directly change a char with my pointer CharPtr ?

    I just want to know how I can do it with pointers.
    No array or string solution please


    Code:
    char *MyString= "Hello World";
    char *CharPtr;
    
    CharPtr= MyString;
    *CharPtr='m';
    
    CharPtr++;
    *CharPtr='o';
    
    cout<<MyString;
    Thanx
    Chris3000

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Litteral strings are in read only memmory and cannot be modified, even by non const pointers.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use
    Code:
    char MyString[] = "Hello World";
    That creates an array large enough for the string (and null terminator) and copies the read-only string into that array. You can then modify MyString.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That they can be assigned to a non-const pointer is only for backwards compatibility with old code, where this was allowed. I can never remember if it's a compiler thing or if the C++ standard actually dictates it for C compatibility ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can never remember if it's a compiler thing or if the C++ standard actually dictates it for C compatibility ...
    It's deprecated, so while the standard still honors its use, that may not be the case in the next revision.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Prelude
    >I can never remember if it's a compiler thing or if the C++ standard actually dictates it for C compatibility ...
    It's deprecated, so while the standard still honors its use, that may not be the case in the next revision.
    really? I can remember it's still allowed because heaps of code had been written using this. I dont think now suddenly 10 years laters where even more heaps of code have been written like that and that they then wanna make it illegal.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    They've done it before. Look at all the code that was written with <iostream.h> and the other pre-standard headers, or the scope of variables created in the initialization of a for loop.

    Everyone's had 10 years to fix code, and plenty of time to learn the const keyword. Even when the standard removes it completely most compilers will probably choose to throw warnings unless you tell it to treat as an error.
    Last edited by Cat; 11-16-2006 at 12:46 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can remember it's still allowed because heaps of code had been written using this.
    That's what deprecation is for. The feature is marked as supported but obsolescent and developers are encouraged to not use it in new code and use a replacement in old code. It's not as if the feature is declared deprecated and then removed after a week or so. It lasts through at least one revision of the standard to give people time to prepare. Full revisions tend to take about ten years, so you can't say you were surprised when a deprecated feature is removed.

    >I dont think now suddenly 10 years laters where even more heaps of code have been written like that and that they then wanna make it illegal.
    Why not? They told you not to use it. If you did anyway, you have only yourself to blame.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM