Sort of mystified from an earlier thread

Discussion in 'C Programming' started by Chad, Oct 30, 2005.

  1. Chad

    Chad Guest

    This was taken from the following:

    http://groups.google.com/group/comp...802b3/663e9afae83d061c?hl=en#663e9afae83d061c

    And I quote:

    "Well, that's also ok for char**, since string literals are of type
    char * in c. The general idea still stands, though.

    The thing that irritates me is that despite all this, it's _trivial_
    to violate const in C without resorting to all this.

    const char foo[] = "mystring";
    char *constviol = strchr(foo,*foo); "

    What I don't get is that that 'const char f[]="mystring" ' is defined
    as a char, but the prototype is defined as the following:

    char *strchr(const char *s, int c);

    When foo gets de-referenced (ie *foo), how come the compiler doesn't
    complain about the difference between 'int' and 'char'?

    Thanks in advance.

    Chad
     
    Chad, Oct 30, 2005
    #1
    1. Advertising

  2. Chad

    pete Guest

    Chad wrote:
    >
    > This was taken from the following:
    >
    > http://groups.google.com/group/comp...802b3/663e9afae83d061c?hl=en#663e9afae83d061c
    >
    > And I quote:
    >
    > "Well, that's also ok for char**, since string literals are of type
    > char * in c. The general idea still stands, though.
    >
    > The thing that irritates me is that despite all this, it's _trivial_
    > to violate const in C without resorting to all this.


    You do realise that string literals represent arrays of char
    and are not pointer types, don't you?

    --
    pete
     
    pete, Oct 30, 2005
    #2
    1. Advertising

  3. Chad

    Jordan Abel Guest

    On 2005-10-30, Chad <> wrote:
    > This was taken from the following:
    >
    > http://groups.google.com/group/comp...802b3/663e9afae83d061c?hl=en#663e9afae83d061c
    >
    > And I quote:


    Keep in mind that when quoting it is customary to indent with a level of >'s

    >> "Well, that's also ok for char**, since string literals are of type
    >> char * in c. The general idea still stands, though.
    >>
    >> The thing that irritates me is that despite all this, it's _trivial_
    >> to violate const in C without resorting to all this.
    >>
    >> const char foo[] = "mystring";
    >> char *constviol = strchr(foo,*foo); "

    >
    > What I don't get is that that 'const char f[]="mystring" ' is defined
    > as a char, but the prototype is defined as the following:
    >
    > char *strchr(const char *s, int c);
    >
    > When foo gets de-referenced (ie *foo), how come the compiler doesn't
    > complain about the difference between 'int' and 'char'?


    char gets promoted implicitly to int

    > Thanks in advance.
    >
    > Chad
    >
     
    Jordan Abel, Oct 30, 2005
    #3
  4. Chad

    Chad Guest

    pete wrote:
    > Chad wrote:
    > >
    > > This was taken from the following:
    > >
    > > http://groups.google.com/group/comp...802b3/663e9afae83d061c?hl=en#663e9afae83d061c
    > >
    > > And I quote:
    > >
    > > "Well, that's also ok for char**, since string literals are of type
    > > char * in c. The general idea still stands, though.
    > >
    > > The thing that irritates me is that despite all this, it's _trivial_
    > > to violate const in C without resorting to all this.

    >
    > You do realise that string literals represent arrays of char
    > and are not pointer types, don't you?
    >
    > --
    > pete


    Yes, I do realize string literals represent array of chars.
     
    Chad, Oct 30, 2005
    #4
    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. Ben Thomas
    Replies:
    7
    Views:
    393
    Kevin Goodsell
    Sep 13, 2003
  2. Chad
    Replies:
    16
    Views:
    471
    Keith Thompson
    Nov 2, 2005
  3. grocery_stocker
    Replies:
    2
    Views:
    643
    Dave Angel
    Apr 19, 2009
  4. Xeno Campanoli
    Replies:
    0
    Views:
    83
    Xeno Campanoli
    May 29, 2008
  5. turnitup

    Mystified....

    turnitup, Sep 15, 2006, in forum: Javascript
    Replies:
    8
    Views:
    110
    Matt Kruse
    Sep 18, 2006
Loading...

Share This Page