473,788 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Meaning of the warning

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
Dec 14 '07 #1
20 2015
somenath <so*********@gm ail.comwrites:
#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
Dec 14 '07 #2
Asbjørn wrote:
) somenath <so*********@gm ail.comwrites:
)
)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
Dec 14 '07 #3
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
Dec 14 '07 #4
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

Dec 14 '07 #5
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
Dec 14 '07 #6
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
Dec 14 '07 #7
In article <Sp************ *********@bt.co m>,
Richard Heathfield <rj*@see.sig.in validwrote:
>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
Dec 14 '07 #8
Richard Tobin said:
In article <Sp************ *********@bt.co m>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>In this case, gcc is confusing "constant" with "const".

... which the user asked it to do, by means of the flag -Wwrite-strings:
Quite. Nevertheless, gcc is confusing "constant" with "const".

--
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
Dec 14 '07 #9
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

Dec 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1844
by: Mechain Marc | last post by:
In one of my logfiles I have quite repeatedly the following message: InnoDB: Warning: using a partial-field key prefix in search What does this warning mean ? Regards, Marc Mechain Atos Origin
4
4120
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan", PRO_REQUIRED }; const char* TOS_network_spantree_set_0_para_1_emvalue = { "disable", "enable", NULL }; PARA TOS_network_spantree_set_0_para_1 = { "", emENUM, TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP", PRO_REQUIRED };
3
2054
by: nick | last post by:
i have 5 files,when i use make command to compile them a error occurs "make: Warning: Infinite loop: Target `c.o' depends on itself" when i type make an warning message occurs cc -c b.c cc -c a.c make: Warning: Infinite loop: Target `c.o' depends on itself cc -c c.c cc b.o a.o c.o -o a
2
34585
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
44
16353
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b, "Built-in == operator should not be
1
5985
by: lavender | last post by:
when I compile my code, it show : WARNING : passing `int' to argument 1 of `deleteQueue(itemType *, queue *)' lacks a cast What is the meaning for this warning? Where I should coorect in my code? void Print(queue *q) { int j; if ((q->tail-q->head) >=5)
5
1869
by: holmescn | last post by:
what is the meaning of warning attributes ignored on template instantiation. i got it when i compiled stlport 5.1.3. anybody can help me ? thx!
22
1839
by: mdh | last post by:
Hi All, Happy Solstice! May I ask the following. The following is a brief excerpt of a practice program. main(...){ if (argc 1 && mystrcomp(argv, "-n") == 0) /* argv is "-n" / ******/ .......}
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.