String Literal Question

Discussion in 'C Programming' started by polas, Oct 8, 2007.

  1. polas

    polas Guest

    Good morning,

    I have a quick question to clear up some confusion in my mind. I
    understand that using a string literal in a declaration such as char
    *p = "string literal" declares a pointer to memory holding the string
    and the string might very well be held in read only memory.

    However, I am sure that I read somewhere that the declaration char a[]
    = "string literal", even though a is an array (and I understand the
    differences between the two declarations), defines a such that it
    might also be also held in read only memory and thus writing to
    indexes of a might not work...

    After some doubt about this popped into my mind, I have had a look
    through the C faq and it seems to suggest that I am wrong - in the
    second case one can use the array declared as above as normal, but I
    would like to make sure.

    Thanks,
    Nick
    polas, Oct 8, 2007
    #1
    1. Advertising

  2. polas said:

    > Good morning,
    >
    > I have a quick question to clear up some confusion in my mind. I
    > understand that using a string literal in a declaration such as char
    > *p = "string literal" declares a pointer to memory holding the string
    > and the string might very well be held in read only memory.


    Right. (Actually, we can say something stronger about p. It isn't merely
    declared; it is actually defined, too.)

    "string literal" is indeed a string literal, stored in (potentially)
    read-only memory, and updating it invokes undefined behaviour (which means
    it might work or it might do something obviously horrible, or it might do
    something very subtle but gerharsterly). All this, you appear to know
    already. So let's move on.

    >
    > However, I am sure that I read somewhere that the declaration char a[]
    > = "string literal", even though a is an array (and I understand the
    > differences between the two declarations), defines a such that it
    > might also be also held in read only memory and thus writing to
    > indexes of a might not work...


    No, the string literal is still a string literal, but your definition of
    the a array reserves fifteen bytes of storage, and ***copies*** the string
    literal (including the null terminator) into that storage, which is
    writeable.

    > After some doubt about this popped into my mind, I have had a look
    > through the C faq and it seems to suggest that I am wrong - in the
    > second case one can use the array declared as above as normal, but I
    > would like to make sure.


    You may rest assured that the array is writeable, yes.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 8, 2007
    #2
    1. Advertising

  3. polas

    polas Guest

    On 8 Oct, 11:33, Richard Heathfield <> wrote:
    > polas said:
    >
    > > Good morning,

    >
    > > I have a quick question to clear up some confusion in my mind. I
    > > understand that using a string literal in a declaration such as char
    > > *p = "string literal" declares a pointer to memory holding the string
    > > and the string might very well be held in read only memory.

    >
    > Right. (Actually, we can say something stronger about p. It isn't merely
    > declared; it is actually defined, too.)
    >
    > "string literal" is indeed a string literal, stored in (potentially)
    > read-only memory, and updating it invokes undefined behaviour (which means
    > it might work or it might do something obviously horrible, or it might do
    > something very subtle but gerharsterly). All this, you appear to know
    > already. So let's move on.
    >
    >
    >
    > > However, I am sure that I read somewhere that the declaration char a[]
    > > = "string literal", even though a is an array (and I understand the
    > > differences between the two declarations), defines a such that it
    > > might also be also held in read only memory and thus writing to
    > > indexes of a might not work...

    >
    > No, the string literal is still a string literal, but your definition of
    > the a array reserves fifteen bytes of storage, and ***copies*** the string
    > literal (including the null terminator) into that storage, which is
    > writeable.
    >
    > > After some doubt about this popped into my mind, I have had a look
    > > through the C faq and it seems to suggest that I am wrong - in the
    > > second case one can use the array declared as above as normal, but I
    > > would like to make sure.

    >
    > You may rest assured that the array is writeable, yes.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    Thanks very much for the reply and information - that clears it up for
    me. The fact that the array actually copies the string literal makes
    sense to me and clears up what I was misunderstanding

    Nick
    polas, Oct 8, 2007
    #3
  4. polas

    Army1987 Guest

    On Mon, 08 Oct 2007 03:21:38 -0700, polas wrote:

    > However, I am sure that I read somewhere that the declaration char a[]
    > = "string literal", even though a is an array (and I understand the
    > differences between the two declarations), defines a such that it
    > might also be also held in read only memory and thus writing to
    > indexes of a might not work...

    The literal in char a[] = "foo"; is a string literal just in order
    not to clobber the syntax with another exception such as the one
    about #include.
    But in practice it is just syntactic sugar for
    char a[] = {'f', 'o', 'o', 0};
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 8, 2007
    #4
  5. On Mon, 08 Oct 2007 10:33:31 +0000, Richard Heathfield
    <> wrote:

    >polas said:
    >
    >> Good morning,
    >>
    >> I have a quick question to clear up some confusion in my mind. I
    >> understand that using a string literal in a declaration such as char
    >> *p = "string literal" declares a pointer to memory holding the string
    >> and the string might very well be held in read only memory.

    >
    >Right. (Actually, we can say something stronger about p. It isn't merely
    >declared; it is actually defined, too.)
    >
    >"string literal" is indeed a string literal, stored in (potentially)
    >read-only memory, and updating it invokes undefined behaviour (which means
    >it might work or it might do something obviously horrible, or it might do
    >something very subtle but gerharsterly). All this, you appear to know
    >already. So let's move on.
    >
    >>
    >> However, I am sure that I read somewhere that the declaration char a[]
    >> = "string literal", even though a is an array (and I understand the
    >> differences between the two declarations), defines a such that it
    >> might also be also held in read only memory and thus writing to
    >> indexes of a might not work...

    >
    >No, the string literal is still a string literal, but your definition of
    >the a array reserves fifteen bytes of storage, and ***copies*** the string
    >literal (including the null terminator) into that storage, which is
    >writeable.


    It is probably an implementation detail as to whether the string
    literal actually exists to be copied into the array or the compiler
    uses another initialization technique to cause the array to contain
    the specified value.

    >
    >> After some doubt about this popped into my mind, I have had a look
    >> through the C faq and it seems to suggest that I am wrong - in the
    >> second case one can use the array declared as above as normal, but I
    >> would like to make sure.

    >
    >You may rest assured that the array is writeable, yes.



    Remove del for email
    Barry Schwarz, Oct 11, 2007
    #5
  6. Barry Schwarz said:

    (in reference to: char arr[] = "initialiser string";)

    > It is probably an implementation detail as to whether the string
    > literal actually exists to be copied into the array or the compiler
    > uses another initialization technique to cause the array to contain
    > the specified value.


    Yes. As far as I can see, a strictly conforming program would not be able
    to tell the difference anyway.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 11, 2007
    #6
  7. On Oct 8, 3:21 pm, polas <> wrote:
    > Good morning,
    >
    > I have a quick question to clear up some confusion in my mind. I
    > understand that using a string literal in a declaration such as char
    > *p = "string literal" declares a pointer to memory holding the string
    > and the string might very well be held in read only memory.
    >
    > However, I am sure that I read somewhere that the declaration char a[]
    > = "string literal", even though a is an array (and I understand the
    > differences between the two declarations), defines a such that it
    > might also be also held in read only memory and thus writing to
    > indexes of a might not work...
    >
    > After some doubt about this popped into my mind, I have had a look
    > through the C faq and it seems to suggest that I am wrong - in the
    > second case one can use the array declared as above as normal, but I
    > would like to make sure.
    >


    Example -
    char *i_reg_fname = "none";

    String constants are sequences of characters enclosed in double
    quotes.
    String literal is the formal term for a double-quoted string in C
    source. 'i_reg_fname' is a pointer to characters.

    Here,
    Note that any attempt to modify the string that 'i_reg_fname' points
    to will result in undefined behaviour.
    that is, i_reg_fname[0]='q'; // not allowed . will cause undefined
    behaviour. :(

    Some compilers have a switch controlling whether string literals are
    writable or not (for compiling old code),
    and some may have options to cause string literals to be formally
    treated as arrays of const char (for better error catching).

    But irrespective of theat, if you have declared as below , then it is
    possible to change.
    char i_reg_fname[] = "none";
    that is, i_reg_fname[0]='q'; // allowed . :)

    i_reg_fname is a non-const pointer to char.
    It may point to a string literal, but it isn't declared const.

    An attempt to modify it will cause undefined behaviour .
    Attempting to modify a string literal invokes undefined
    behavior, because the C standard defines that attempting to modify a
    string literal invokes undefined behavior.
    It is because of the C standard and it is not 'const'.

    In actual practice, the behaviour depends on where the compiler
    decides to put its string constants. Some compilers have a switch
    controlling whether
    string literals are writable or not (for compiling old code),and some
    may have options to
    cause string literals to be formally treated as arrays of const char
    (for better
    error catching).

    Earlier C didn not have the 'const' keyword, so if you wanted to
    pass
    a string literal to a particular function( In sucha a way that the
    string will
    not be modified inside the
    function), then that particular function must take a 'char*'
    argument.
    Thats all.

    Lot of information is available regarding this String Literal in
    internet and groups. Also read the C-Faq (question 1.32 will give you
    enough info).

    Refer :- http://groups.google.co.in/group/comp.lang.c/browse_thread/thread/5a67581e333a3e04/?hl=en#

    Karthik Balaguru
    karthikbalaguru, Oct 11, 2007
    #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. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    624
    John C. Bollinger
    Aug 27, 2003
  2. Replies:
    5
    Views:
    717
    Frank Schmitt
    Sep 22, 2003
  3. herrcho
    Replies:
    7
    Views:
    412
    Jack Klein
    Sep 26, 2003
  4. Replies:
    10
    Views:
    751
    Roland Pibinger
    Jan 24, 2007
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    362
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page