Thread: string literal assignment

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    string literal assignment

    Can string literals be used for assignment other than in an initializer?
    Code:
    int main( void ) {
    char *p = "foo";
    p = "bar";
    }
    (disregarding the loss of static storage) even with -Wall, this compiles with no warnings/error; though I could have sworn I had read somewhere in 9899 they could be used for initialization only...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It depends. This code assigns the address of a string literal to a pointer (which really should be const char*), so it is fine.
    However, trying to assign a string literal to a buffer, eg:
    char foo[] = "Hello World!";
    This can only be done with initialization.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM