Waring for string is absent.

Discussion in 'C Programming' started by shaanxxx, Aug 26, 2006.

  1. shaanxxx

    shaanxxx Guest

    void foo(char * str)
    str[0] = str[0];

    int main()

    const char str[] = "Hello";
    foo(str); // i get warning here
    foo("shaan"); // i dont get warning here.


    should i interpret above programme as, String returns pointer to char
    (which is non-constant).
    Above statement is correct ?
    shaanxxx, Aug 26, 2006
    1. Advertisements

  2. shaanxxx

    Eric Sosman Guest

    You get a warning on the first call to foo because
    str (in main) is a `const' string, but the argument of
    foo is not `const'. "Adding" a restriction is harmless,
    but "subtracting" a restriction generates a diagnostic.

    You do not get a warning on the second call because
    the literal "shaan" generates a string that is not `const'.
    That is an accident of history, having to do with the way
    C developed and was used before the `const' keyword came
    into the language. The peculiar outcome is that a string
    constant is not `const', but you must act as if it were.

    Both calls to foo invoke undefined behavior by trying
    to modify the string:

    - In the first case the string is actually `const' and
    it is undefined behavior to attempt to modify a
    `const' object.

    - In the second case the string is not `const', but it
    is undefined behavior to attempt to modify a string

    And yes: Replacing a character with itself is in fact an
    attempt to "modify" the string, even though the modification
    (if it succeeded) would give the string the same content as
    it had before.
    Sorry; I do not understand this.
    Eric Sosman, Aug 26, 2006
    1. Advertisements

  3. shaanxxx

    shaanxxx Guest

    I got the answer. I wanted to ask, why 'string' is not define as const.

    shaanxxx, Aug 26, 2006
  4. shaanxxx

    Simon Biber Guest

    'string' in C refers to a particular layout in memory: a contiguous
    block of 'char' values, ending with a zero value. The zero value is
    called a null character.

    You can have const strings and non-const strings. More precisely, you
    can declare an array to hold const data or non-const data, and you can
    access a string through a 'pointer to const char' or a 'pointer to
    non-const char'.

    foo is an array of const char, containing a string.

    char const foo[] = {'h', 'e', 'l', 'l', 'o', 0};

    bar is an array of non-const char, containing a string.

    char bar[] = {'w', 'o', 'r', 'l', 'd', 0};

    You may modify the contents of foo, but not bar.
    Simon Biber, Aug 26, 2006
  5. shaanxxx

    Eric Sosman Guest

    I think you mean "bar, but not foo."
    Eric Sosman, Aug 26, 2006
  6. shaanxxx

    Simon Biber Guest

    Simon Biber, Aug 26, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.