Re: another const question

Discussion in 'C Programming' started by Emmanuel Delahaye, Jun 29, 2003.

  1. Emmanuel Delahaye, Jun 29, 2003
    #1
    1. Advertising

  2. Emmanuel Delahaye

    Ben Pfaff Guest

    Emmanuel Delahaye <> writes:

    > In 'comp.lang.c', (Felix Zaslavskiy) wrote:
    >
    > > char c[] = "hello";
    > >
    > > Can one say that c is type of const char * ?

    >
    > No. For that, you have to be explicit:
    >
    > char const c[] = "hello";


    That's still not of type const char *. For that, you have to
    declare it to be of type const char *:
    const char *c = "hello";
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Jun 29, 2003
    #2
    1. Advertising

  3. Emmanuel Delahaye

    Jason Xie Guest

    "Ben Pfaff" <>
    ??????:...
    > Emmanuel Delahaye <> writes:
    >
    > > In 'comp.lang.c', (Felix Zaslavskiy) wrote:
    > >
    > > > char c[] = "hello";
    > > >
    > > > Can one say that c is type of const char * ?

    > >
    > > No. For that, you have to be explicit:
    > >
    > > char const c[] = "hello";

    >
    > That's still not of type const char *. For that, you have to
    > declare it to be of type const char *:
    > const char *c = "hello";


    How about this:
    const char c[] = "hello";
    I always use it as a const string :)

    > --
    > int main(void){char

    p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    > \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int

    putchar(\
    > );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof

    p-1;putchar(p\
    > );}return 0;}
     
    Jason Xie, Jun 30, 2003
    #3
  4. Emmanuel Delahaye

    Simon Biber Guest

    "Jason Xie" <> wrote:
    > How about this:
    > const char c[] = "hello";
    > I always use it as a const string :)


    c has type "const char [6]", that is an array of 6 const chars.
    The array c holds a string.

    When used in an expression (other than as the operand of the
    'unary &' or 'sizeof' operators) it decays into a value of type
    "const char *", that is a pointer to const char, which is a
    pointer to the first element of the array. This pointer is a
    pointer to a string.

    --
    Simon.
     
    Simon Biber, Jun 30, 2003
    #4
  5. "Simon Biber" <> wrote in message news:<3effaa44$0$5973$>...
    > "Jason Xie" <> wrote:
    > > How about this:
    > > const char c[] = "hello";
    > > I always use it as a const string :)

    >
    > c has type "const char [6]", that is an array of 6 const chars.
    > The array c holds a string.
    >
    > When used in an expression (other than as the operand of the
    > 'unary &' or 'sizeof' operators) it decays into a value of type
    > "const char *", that is a pointer to const char, which is a
    > pointer to the first element of the array. This pointer is a
    > pointer to a string.


    i though another way of saying decays into something as implicit converted.
     
    Felix Zaslavskiy, Jul 3, 2003
    #5
  6. Emmanuel Delahaye

    Micah Cowan Guest

    (Felix Zaslavskiy) writes:

    > "Simon Biber" <> wrote in message news:<3effaa44$0$5973$>...
    > > "Jason Xie" <> wrote:
    > > > How about this:
    > > > const char c[] = "hello";
    > > > I always use it as a const string :)

    > >
    > > c has type "const char [6]", that is an array of 6 const chars.
    > > The array c holds a string.
    > >
    > > When used in an expression (other than as the operand of the
    > > 'unary &' or 'sizeof' operators) it decays into a value of type
    > > "const char *", that is a pointer to const char, which is a
    > > pointer to the first element of the array. This pointer is a
    > > pointer to a string.

    >
    > i though another way of saying decays into something as implicit converted.


    You thought right.

    -Micah
     
    Micah Cowan, Jul 4, 2003
    #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. Replies:
    11
    Views:
    1,108
  2. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,122
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page