Like Tree2Likes
  • 1 Post By MK27
  • 1 Post By ledow

const char* reassignment

This is a discussion on const char* reassignment within the C Programming forums, part of the General Programming Boards category; I've just realized that something I've always assumed is illegal might not be. Code: const char* s = "okay"; [...] ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    const char* reassignment

    I've just realized that something I've always assumed is illegal might not be.

    Code:
    const char* s = "okay";
    [...]
    s = "something else";
    [...]
    s = "and so on";
    Can you re-assign different string literals to a const char* this way?
    Epy likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Yeah absolutely!

    You would only not be able to do that if you declared s as:
    Code:
    const char*const s = "You're stuck with me";
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    The data *pointed at* by the pointer cannot be changed. But the data pointer can be changed to point to somewhere else.

    Quite a common "gotcha", especially in functions that takes things like that as parameters. As iMalc says, if you declare both the pointer AND the data it points at as const, then it will fail as expected.
    CommonTater likes this.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Huh. Well, good I guess, live and learn.

    I understand the stuff about const pointer vs. const data. It just suddenly struck me that the literal itself might be compiled into the read-only data segment, and not stack (which would potentially take it out of scope). For whatever reason, I'd been thinking that only happened when a const char pointer is initialized with a literal.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 03:57 AM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21