string constant

Discussion in 'C Programming' started by chandanlinster, Oct 20, 2006.

  1. hello everybody,

    consider the following statement,
    char *s = "someString";

    Does the above statement cause "someString" to be alloted a constant
    memory space. What I mean is can't we manipulate "someString" using
    statements like

    s[0] = 'a';
    chandanlinster, Oct 20, 2006
    #1
    1. Advertising

  2. chandanlinster <> wrote:

    > consider the following statement,
    > char *s = "someString";


    > Does the above statement cause "someString" to be alloted a constant
    > memory space.


    It might; implementations are allowed to make this choice for
    themselves.

    > What I mean is can't we manipulate "someString" using
    > statements like


    > s[0] = 'a';


    No. Not portably, at least, although some implementations offer the
    ability to modify string literals as an extension.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 20, 2006
    #2
    1. Advertising

  3. "chandanlinster" <> writes:
    > consider the following statement,
    > char *s = "someString";
    >
    > Does the above statement cause "someString" to be alloted a constant
    > memory space. What I mean is can't we manipulate "someString" using
    > statements like
    >
    > s[0] = 'a';


    No, attempting to modify a string literal invokes undefined behavior.

    (For historical reasons, string literals are not treated as "const";
    attempting to modify one invokes UB because the standard says so
    explicitly.)

    Some implementations may allow you to do this. Don't.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 20, 2006
    #3
  4. chandanlinster

    Ian Collins Guest

    Keith Thompson wrote:
    > "chandanlinster" <> writes:
    >
    >>consider the following statement,
    >> char *s = "someString";
    >>
    >>Does the above statement cause "someString" to be alloted a constant
    >>memory space. What I mean is can't we manipulate "someString" using
    >>statements like
    >>
    >> s[0] = 'a';

    >
    >
    > No, attempting to modify a string literal invokes undefined behavior.
    >
    > (For historical reasons, string literals are not treated as "const";
    > attempting to modify one invokes UB because the standard says so
    > explicitly.)
    >

    But it's still worth declaring them as const.

    --
    Ian Collins.
    Ian Collins, Oct 20, 2006
    #4
  5. Ian Collins <> writes:
    > Keith Thompson wrote:
    >> "chandanlinster" <> writes:
    >>
    >>>consider the following statement,
    >>> char *s = "someString";
    >>>
    >>>Does the above statement cause "someString" to be alloted a constant
    >>>memory space. What I mean is can't we manipulate "someString" using
    >>>statements like
    >>>
    >>> s[0] = 'a';

    >>
    >>
    >> No, attempting to modify a string literal invokes undefined behavior.
    >>
    >> (For historical reasons, string literals are not treated as "const";
    >> attempting to modify one invokes UB because the standard says so
    >> explicitly.)
    >>

    > But it's still worth declaring them as const.


    Yes.

    More pedantically, if you have an array whose initializer is a string
    literal, it's a good idea to declare the array as const.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 21, 2006
    #5
  6. chandanlinster

    Eric Sosman Guest

    Keith Thompson wrote:
    > Ian Collins <> writes:
    >
    >>Keith Thompson wrote:
    >>
    >>>"chandanlinster" <> writes:
    >>>
    >>>
    >>>>consider the following statement,
    >>>> char *s = "someString";
    >>>>
    >>>>Does the above statement cause "someString" to be alloted a constant
    >>>>memory space. What I mean is can't we manipulate "someString" using
    >>>>statements like
    >>>>
    >>>> s[0] = 'a';
    >>>
    >>>
    >>>No, attempting to modify a string literal invokes undefined behavior.
    >>>
    >>>(For historical reasons, string literals are not treated as "const";
    >>>attempting to modify one invokes UB because the standard says so
    >>>explicitly.)
    >>>

    >>
    >>But it's still worth declaring them as const.

    >
    >
    > Yes.
    >
    > More pedantically, if you have an array whose initializer is a string
    > literal, it's a good idea to declare the array as const.


    Wouldn't that depend on the purpose of the array?

    /* const ??? */ char devname[] = "/dev/pty?";
    char *p = strchr(devname, '?');
    char *q;
    FILE *stream;
    for (q = "abc"; *q != '\0'; ++q) {
    *p = *q;
    stream = fopen(devname, "r+");
    if (stream != NULL)
    return stream;
    }
    return NULL;

    In my experience, an explicit array is initialized with a
    string literal only when one *does* want to modify it; if one
    does not, one simply uses the literal. Do you write

    const char message[] = "Hello, world!";
    puts (message);

    or simply

    puts ("Hello, world!");

    ?

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 21, 2006
    #6
  7. Eric Sosman <> writes:
    > Keith Thompson wrote:

    [...]
    >> More pedantically, if you have an array whose initializer is a string
    >> literal, it's a good idea to declare the array as const.

    >
    > Wouldn't that depend on the purpose of the array?

    [snip]

    Yes, sorry, I goofed.

    If you have a *pointer* whose initializer is a string literal, you
    should declare it as const:

    const char *p = "hello";

    If you have an *array*, declare it as const or not depending on how
    you want to use it; the initializer is irrelevant:

    char arr[] = "hello";

    Here, the string literal is (logically) *copied* to the array. In the
    first case, the pointer actually points to the string literal itself.
    More precisely, the string literal causes an anonymous array of static
    storage duration, just large enough to hold the sequence of
    characters, to be created. The pointer points to this array. Any
    attempt to modify the array invokes UB.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 21, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,066
  2. Martin Magnusson
    Replies:
    2
    Views:
    483
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    534
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    329
    Keith Thompson
    Dec 14, 2006
  5. sinbad
    Replies:
    7
    Views:
    635
    sinbad
    Jun 19, 2008
Loading...

Share This Page