Thread: char *a="123"; a[1]='A';//In VC++ has error, TCPP doesn't have

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    Question char *a="123"; a[1]='A';//In VC++ has error, TCPP doesn't have

    strange problem,

    Code:
    int main(void){
      char *a = "123456";
    
      a[2] = 'a' ;
    
      printf( "%s", a );
    
      return 0;
    }
    When compiled in VC++, there is no compile time error but a memory error at run-time.

    In TCPP 3.0, there is no problem at all.

    what is the problem ? THANKS~
    Last edited by L.O.K.; 12-03-2004 at 11:28 AM.

  2. #2
    Hello,

    The variable a is a string literal. That variable is initialized as a string literal and may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents.

    Either define a as an array, or allocate a block of memory to it.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    To expand on that, since string literals are not meant to be modified, the compiler is free to put string literals in read-only memory, in which case naturally you won't be able to modify them without causing runtime errors.

    As Stack Overflow has mentioned, the simplest fix is to do:
    char a[] = "123456";
    instead of
    char* a = "123456";

    The reason it works in one compiler and not the other, is simply because the behaviour is undefined: It may or may not work, depending on what the compiler decides to do; and since there are no rules on what the compiler is supposed to do in this situation, it may do anything.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    thank..~

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is simply because the behaviour is undefined
    Actually, it's implementation specific - your compiler docs should state whether strings are in read-only memory or not.

    For example, gcc has "-fwritable-strings" as a command line option.

    But the default should be to make strings read-only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. makefile exported by vc 6.0 doesn't work
    By wow in forum Windows Programming
    Replies: 7
    Last Post: 03-24-2006, 04:20 PM
  2. Can't compile this with VC 6.0
    By uriel2013 in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 07:43 PM
  3. Why VC?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 04-15-2002, 05:24 AM