somenath said:
<snip>
> char *abc ="Hello";
>
<snip>
> jj.c:5: warning: initialization discards qualifiers from pointer
> target type
>
> I have two question
> 1) What is the meaning of the warning ?
There are, alas, no restrictions on the amount of gibberish that
implementations are allowed to produce when translating a program.
In this case, gcc is confusing "constant" with "const". The string literal,
"Hello", is an array of 6 char (so it has type char[6]), with static
storage duration, and it's "constant" in the sense that, if you try to
modify it, the behaviour is undefined. But it is *not* const. The
assignment doesn't discard any qualifiers at all.
gcc means well - it's trying to stop you from hurting yourself by pointing
an ordinary non-const-qualified char * to a string literal, which is
invariably a bad idea - but the wording of the warning is broken.
> 2) is it safe to return the local pointer value (i.e return abc
is
> correct ?
It's unwise. Use const char * when pointing at string literals.
> My understanding is we should not return address of local
> variable .So above code may not be working always .
String literals are not variables, and their lifetime is not restricted to
that of the function (if any) in which they appear.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999