Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: [lpc2000] Re: Problems with sting constants and gcc -On

2005-10-18 by Robert Adsett

At 10:14 AM 10/18/05 +0200, Sten wrote:
>why you do you declare a string in that way?
>
>         char * f = "Hello";
>
>'char* f' declares only a pointer to something. In this declaration
>"Hello" should be treated as constant char array by the compiler. Does
>the compiler complains about assigning const to non-const if you turn on
>-Wall???

Actually due to historical reasons it "Hello" doesn't get treated as a 
constant char array type.  I'd prefer it that way but...

Some implementations though will treat "hello" as a constant string in 
which case the pointer type and what it points to would be in conflict.  I 
don't think the standard allows that but I don't know and I certainly don't 
know if any newer compilers do that.  I do know that some pre-standard 
compilers would put the string in a constant area and that would cause some 
problems for programs that assumed they were modifiable.

>Correct way to declare a pre-defined char array containing "Hello" would be:
>
>         char f[] = "Hello";
>
>That's a different! May the first line result in some confusions in the
>compiler?!?

Not if it's standards compliant.  That doesn't mean it's impossible but it 
would be a pretty big point to mess up.  Either form is 'correct' .  Which 
one you choose is a matter of choice, style and the specific 
circumstance.  Personally I would rarely use either one without a const 
qualifier.

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,   be 
they legal, genetic, or physical.  If you don't believe me, try to chew a 
radio signal. "  -- Kelvin Throop, III
http://www.aeolusdevelopment.com/

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.