Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Meaning of the warning

Reply
Thread Tools

Meaning of the warning

 
 
somenath
Guest
Posts: n/a
 
      12-14-2007
Hi All,
I have one question regarding the code.


#include<stdio.h>
char *f1(void);
char *f1(void)
{
char *abc ="Hello";

return abc;
}

int main(void )
{
char *p;
p = f1();

printf("%s\n", p);
return 0;
}


While compiling the above program as mentioned below I am getting the
meaning
$ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2
jj.c
jj.c: In function `f1':
jj.c:5: warning: initialization discards qualifiers from pointer
target type

I have two question
1) What is the meaning of the warning ?
2) is it safe to return the local pointer value (i.e return abc is
correct ?

My understanding is we should not return address of local
variable .So above code may not be working always .

Regards,
Somenath
 
Reply With Quote
 
 
 
 
Asbjørn Sæbø
Guest
Posts: n/a
 
      12-14-2007
somenath <> writes:

> #include<stdio.h>
> char *f1(void);
> char *f1(void)
> {
> char *abc ="Hello";
>
> return abc;
> }
>
> int main(void )
> {
> char *p;
> p = f1();
>
> printf("%s\n", p);
> return 0;
> }
>
>
> While compiling the above program as mentioned below I am getting the
> meaning
> $ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
> Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
> prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
> Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2
> jj.c
> jj.c: In function `f1':
> jj.c:5: warning: initialization discards qualifiers from pointer
> target type
>
> I have two question
> 1) What is the meaning of the warning ?


May it have anything to do with the signed-ness of char and the
signed-ness of "Hello"?

Asbjørn
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      12-14-2007
Asbjørn wrote:
) somenath <> writes:
)
)> char *f1(void)
)> {
)> char *abc ="Hello";
)>
)> return abc;
)> }
)> <snip>
)> jj.c:5: warning: initialization discards qualifiers from pointer
)> target type
)>
)> I have two question
)> 1) What is the meaning of the warning ?
)
) May it have anything to do with the signed-ness of char and the
) signed-ness of "Hello"?

Unlikely. My first guess would rather be the const-ness.

About question 2): Is the above legal code ?

Yes it is. You're not returning the address of a local variable,
but that of a string literal. It's basically the same as:

char *f1(void)
{
return "Hello";
}

Which might give a similar warning, or might not.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-14-2007
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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-14-2007
somenath wrote:
>
> Hi All,
> I have one question regarding the code.
>
> #include<stdio.h>
> char *f1(void);
> char *f1(void)
> {
> char *abc ="Hello";
>
> return abc;
> }
>
> int main(void )
> {
> char *p;
> p = f1();
>
> printf("%s\n", p);
> return 0;
> }
>
> While compiling the above program as mentioned below I am getting the
> meaning
> $ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
> Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
> prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
> Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2
> jj.c
> jj.c: In function `f1':
> jj.c:5: warning: initialization discards qualifiers from pointer
> target type
>
> I have two question
> 1) What is the meaning of the warning ?


Spurious.

> 2) is it safe to return the local pointer value (i.e return abc is
> correct ?
>
> My understanding is we should not return address of local
> variable .So above code may not be working always .


"abc" isn't a local pointer value.
"abc" has static duration. It exists from program start to end.

Your code is flawless. The warning is garbage.

I suspect that your compiler thinks that the type of ("abc" + 0)
is supposed to be (const char *),
but in C, the type of ("abc" + 0) is (char *).

In C,
the type of ("abc") is (char [4]),
not (const char [4]).

--
pete
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-14-2007
somenath wrote:

> Hi All,
> I have one question regarding the code.
>
> #include<stdio.h>
> char *f1(void);
> char *f1(void)
> {
> char *abc ="Hello";
> return abc;
> }
>
> int main(void )
> {
> char *p;
> p = f1();
> printf("%s\n", p);
> return 0;
> }
>
>
> While compiling the above program as mentioned below I am getting the
> meaning
> $ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
> Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
> prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
> Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2
> jj.c
> jj.c: In function `f1':
> jj.c:5: warning: initialization discards qualifiers from pointer
> target type
>
> I have two question
> 1) What is the meaning of the warning ?


Since you compiled with -Wwrite-strings, gcc non-Standardly makes
string literals be of type `const char *` rather than `char *`.
Then, when you try and assign the `const char *` to the `char *`
variable `abc`, it correctly complains: you've discarded the const
qualifier and left yourself open to someone changing something
which they're not supposed to change.

If you don't want this behaviour, don't ask for it.

> 2) is it safe to return the local pointer value (i.e return abc is
> correct ?


That isn't a "local pointer value"; it's the value of a local variable,
but that value is a pointer-to-string-literal, and a string literal
is a static and has lifetime equal to that of the entire executing
program.

> My understanding is we should not return address of local
> variable.


You shouldn't, and you aren't.

--
Chris "doctor, doctor, it hurts when I do /this/!" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-14-2007
pete wrote:
>
> somenath wrote:
> >
> > Hi All,
> > I have one question regarding the code.
> >
> > #include<stdio.h>
> > char *f1(void);
> > char *f1(void)
> > {
> > char *abc ="Hello";
> >
> > return abc;
> > }
> >
> > int main(void )
> > {
> > char *p;
> > p = f1();
> >
> > printf("%s\n", p);
> > return 0;
> > }
> >
> > While compiling the above program as mentioned below I am getting the
> > meaning
> > $ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
> > Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
> > prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
> > Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2
> > jj.c
> > jj.c: In function `f1':
> > jj.c:5: warning: initialization discards qualifiers from pointer
> > target type
> >
> > I have two question
> > 1) What is the meaning of the warning ?

>
> Spurious.
>
> > 2) is it safe to return the local pointer value (i.e return abc is
> > correct ?
> >
> > My understanding is we should not return address of local
> > variable .So above code may not be working always .

>
> "abc" isn't a local pointer value.
> "abc" has static duration. It exists from program start to end.
>
> Your code is flawless. The warning is garbage.
>
> I suspect that your compiler thinks that the type of ("abc" + 0)
> is supposed to be (const char *),
> but in C, the type of ("abc" + 0) is (char *).
>
> In C,
> the type of ("abc") is (char [4]),
> not (const char [4]).


I forgot that we were talking about ("Hello") and not ("abc")

I suspect that your compiler thinks that the type of ("Hello" + 0)
is supposed to be (const char *),
but in C, the type of ("Hello" + 0) is (char *).

In C,
the type of ("Hello") is (char [6]),
not (const char [6]).

--
pete
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-14-2007
somenath wrote:
>
> #include<stdio.h>
> char *f1(void);
> char *f1(void) {
> char *abc ="Hello";
> return abc;
> }
>
> int main(void ) {
> char *p;
> p = f1();
>
> printf("%s\n", p);
> return 0;
> }
>
> While compiling the above program as mentioned below I am getting
> the meaning

.... snip ...
> jj.c:5: warning: initialization discards qualifiers from pointer
> target type
>
> I have two question
> 1) What is the meaning of the warning ?
> 2) is it safe to return the local pointer value (i.e return abc
> is correct ?
>
> My understanding is we should not return address of local
> variable .So above code may not be working always .


Change "char *abc ="Hello";" to "const char *abc ="Hello";". You
are not returning a pointer to local storage, you are returning the
value of that local storage.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      12-14-2007
In article <>,
Richard Heathfield <> wrote:

>In this case, gcc is confusing "constant" with "const".


.... which the user asked it to do, by means of the flag -Wwrite-strings:

`-Wwrite-strings'
When compiling C, give string constants the type `const
char[LENGTH]' so that copying the address of one into a
non-`const' `char *' pointer will get a warning; when compiling
C++, warn about the deprecated conversion from string constants to
`char *'. These warnings will help you find at compile time code
that can try to write into a string constant, but only if you have
been very careful about using `const' in declarations and
prototypes. Otherwise, it will just be a nuisance; this is why we
did not make `-Wall' request these warnings.

-- Richard
--
:wq
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-14-2007
pete wrote:
> pete wrote:
>> somenath wrote:
>>

.... snip ...
>>
>>> $ gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -
>>> Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-
>>> prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -
>>> Wcast-qual -Wshadow -Wconversion -Wwrite-strings -ffloat-store -O2

^^^^^^^^^^^^^^^
.... snip ...
>>
>> Spurious.


Not spurious.

.... snip ...
>>
>> Your code is flawless. The warning is garbage.


The code is flawed. The warnings are accurate.

>> I suspect that your compiler thinks that the type of ("abc" + 0)
>> is supposed to be (const char *), but in C, the type of
>> ("abc" + 0) is (char *).


Look up the meaning of the underlined gcc option above.

.... snip ...
>
> In C, the type of ("Hello") is (char [6]), not (const char [6]).


Not when it has been told otherwise.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
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
Understanding search queries, semantics, and "Meaning" ...aren't weall looking for meaning? 5lvqbwl02@sneakemail.com Python 4 01-14-2009 02:28 PM
warning's meaning holmescn C++ 5 06-03-2007 02:42 PM
what is the meaning of the Warning message,why it occurs? nick C Programming 3 10-12-2005 06:11 PM
Re: what's meaning of "warning: initializer element is not computable at load time" bingfeng C Programming 3 06-06-2005 04:49 AM
what's meaning of "warning: initializer element is not computable at load time" bingfeng C Programming 4 06-02-2005 01:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57