Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: another const question

Reply
Thread Tools

Re: another const question

 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-29-2003
In 'comp.lang.c', http://www.velocityreviews.com/forums/(E-Mail Removed) (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";

defines a read only string "hello" "named 'c'

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      06-29-2003
Emmanuel Delahaye <(E-Mail Removed)> writes:

> In 'comp.lang.c', (E-Mail Removed) (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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
Jason Xie
Guest
Posts: n/a
 
      06-30-2003

"Ben Pfaff" <(E-Mail Removed)>
??????:(E-Mail Removed)...
> Emmanuel Delahaye <(E-Mail Removed)> writes:
>
> > In 'comp.lang.c', (E-Mail Removed) (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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
> \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[i]\
> );}return 0;}



 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      06-30-2003
"Jason Xie" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Felix Zaslavskiy
Guest
Posts: n/a
 
      07-03-2003
"Simon Biber" <(E-Mail Removed)> wrote in message news:<3effaa44$0$5973$(E-Mail Removed) u>...
> "Jason Xie" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      07-04-2003
(E-Mail Removed) (Felix Zaslavskiy) writes:

> "Simon Biber" <(E-Mail Removed)> wrote in message news:<3effaa44$0$5973$(E-Mail Removed) u>...
> > "Jason Xie" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments