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
    1. Advertising

  2. shaanxxx

    Eric Sosman Guest

    shaanxxx wrote:

    > 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.
    >
    > }


    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
    constant.

    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.

    > should i interpret above programme as, String returns pointer to char
    > (which is non-constant).
    > Above statement is correct ?


    Sorry; I do not understand this.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 26, 2006
    #2
    1. Advertising

  3. shaanxxx

    shaanxxx Guest

    Eric Sosman wrote:

    >
    > Sorry; I do not understand this.
    >

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


    Thanks,
    Shaan.
     
    shaanxxx, Aug 26, 2006
    #3
  4. shaanxxx

    Simon Biber Guest

    shaanxxx wrote:
    > Eric Sosman wrote:
    >
    >> Sorry; I do not understand this.
    >>

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


    '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.
     
    Simon Biber, Aug 26, 2006
    #4
  5. shaanxxx

    Eric Sosman Guest

    Simon Biber wrote:

    > shaanxxx wrote:
    >
    >> Eric Sosman wrote:
    >>
    >>> Sorry; I do not understand this.
    >>>

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

    >
    >
    > '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.


    I think you mean "bar, but not foo."

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 26, 2006
    #5
  6. shaanxxx

    Simon Biber Guest

    Eric Sosman wrote:
    > Simon Biber wrote:
    >> 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.

    >
    > I think you mean "bar, but not foo."


    Indeed.

    --
    Simon.
     
    Simon Biber, Aug 26, 2006
    #6
    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. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,268
    Stanimir Stamenkov
    Apr 25, 2005
  2. kyle.tk

    Waring about pointers

    kyle.tk, Feb 17, 2006, in forum: C Programming
    Replies:
    5
    Views:
    365
  3. karthikbalaguru

    String Constant absent in C++

    karthikbalaguru, Sep 2, 2007, in forum: C++
    Replies:
    30
    Views:
    995
    Charles Richmond
    Sep 7, 2007
  4. karthikbalaguru

    String Constant absent in C++

    karthikbalaguru, Sep 2, 2007, in forum: C Programming
    Replies:
    27
    Views:
    779
    Charles Richmond
    Sep 7, 2007
  5. chaitanya.531

    waring in vhdl Xst:1355

    chaitanya.531, Mar 1, 2012, in forum: VHDL
    Replies:
    0
    Views:
    786
    chaitanya.531
    Mar 1, 2012
Loading...

Share This Page