Re: How to initialize a char*?
"Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
news:c5hbdn$e0l $1@chessie.cirr .com...[color=blue]
> Peter <ybi10@yahoo.co m> spoke thus:
>[color=green]
> > char* ptr;
> > func(..., ptr);[/color]
>
> This is bad - ptr is uninitialized (not even NULL), so passing it to a
> function is either pointless or an invitation to disaster, a mistake
> in any case.[/color]
Why?
[color=blue][color=green]
> > How to properly initialize a char*? I used to use[/color]
>[color=green]
> > char* ptr = "";[/color]
>
> Depends on what you're doing with it. You probably were told that
> because it should be
>
> const char *ptr="";[/color]
And what would be the use of that?
[color=blue]
> Why? Because "" is a constant empty string; trying to modify it
> invites disaster. Therefore, pointers to such string literals (so
> they're called) should be declared const; that way if you try to
> modify what it points to the compiler can inform you of your mistake.[/color]
The above is entirely, completely useless.
Ioannis Vranos
"Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
news:c5hbdn$e0l $1@chessie.cirr .com...[color=blue]
> Peter <ybi10@yahoo.co m> spoke thus:
>[color=green]
> > char* ptr;
> > func(..., ptr);[/color]
>
> This is bad - ptr is uninitialized (not even NULL), so passing it to a
> function is either pointless or an invitation to disaster, a mistake
> in any case.[/color]
Why?
[color=blue][color=green]
> > How to properly initialize a char*? I used to use[/color]
>[color=green]
> > char* ptr = "";[/color]
>
> Depends on what you're doing with it. You probably were told that
> because it should be
>
> const char *ptr="";[/color]
And what would be the use of that?
[color=blue]
> Why? Because "" is a constant empty string; trying to modify it
> invites disaster. Therefore, pointers to such string literals (so
> they're called) should be declared const; that way if you try to
> modify what it points to the compiler can inform you of your mistake.[/color]
The above is entirely, completely useless.
Ioannis Vranos
Comment