473,799 Members | 2,972 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
20 2018
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

Dec 14 '07 #11
In article <iO************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>Quite. Nevertheless, gcc is confusing "constant" with "const".
Perhaps "approximat ing" would be a better term.

-- Richard
--
:wq
Dec 14 '07 #12
On Dec 14, 9:09 pm, CBFalconer <cbfalco...@yah oo.comwrote:
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.
Just to clarify myself I would like to explain my understanding.
In the definition char *abc ="Hello";
Say 'H' is stored in address 100 and the 'e' will be stored in 108 and
so on . Now "abc" will have the value 100. When "return abc" is
getting executed it will try to return 100 . Is it not correct? If it
is so I am trying to return a address obtained locally. So it should
be wrong. I think I am missing the knowledge about string literal . Is
it handled specially than local variable .
Suppose
int x = 5;
return &x;
According to me we should not this, as address of x will be scrapped
after the execution flow returns from particular function.
Dec 14 '07 #13
somenath said:
On Dec 14, 9:09 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>somenath wrote:
<snip>
char *abc ="Hello";
return abc;
<snip>
My understanding is we should not return address of local
variable .So above code may not be working always .

[...] You are not returning a pointer to local storage, you
are returning the value of that local storage.

Just to clarify myself I would like to explain my understanding.
In the definition char *abc ="Hello";
Say 'H' is stored in address 100 and the 'e' will be stored in 108 and
so on .
The byte is the smallest addressable unit of storage, and an array occupies
a contiguous block of bytes. If 'H' is in address 100, 'e' is going to be
in address 101.
Now "abc" will have the value 100. When "return abc" is
getting executed it will try to return 100 . Is it not correct?
Yes, that's right, although of course the 100 will have pointer type.
If it
is so I am trying to return a address obtained locally.
Yes, but it isn't the address of an automatic object, i.e. an object that
will automatically be destroyed on scope exit. Rather, it is the address
of a string literal, which survives throughout the lifetime of the program
and doesn't move around in memory (in the abstract machine, that is).
So it should be wrong.
No, but I understand why you're worried, so let's deal with that next.
I think I am missing the knowledge about string literal . Is
it handled specially than local variable .
No, it's just that it's not a local variable - it's a completely different
animal.
Suppose
int x = 5;
return &x;
According to me we should not this, as address of x will be scrapped
after the execution flow returns from particular function.
You are right that we shouldn't do this, but it's not because the address
will be scrapped, so much as that it will become meaningless (because the
object at that address is (conceptually) destroyed when the function
returns). The point of this (conceptual) destruction is that it makes room
for further automatic variables to be constructed during subsequent
processing.

--
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 #14
somenath wrote:
Just to clarify myself I would like to explain my understanding.
In the definition char *abc ="Hello";
Say 'H' is stored in address 100 and the 'e' will be stored in 108 and
so on .
Well, that's mildly unlikely, `sizeof(char)` being 1, unless you
have 8-bit chars on a bit-addressed machine.
Now "abc" will have the value 100. When "return abc" is
getting executed it will try to return 100 . Is it not correct?
For values of `100` which are pointers.
If it is so I am trying to return a address obtained locally.
Nothing wrong with that. It's /exporting the address of an automatic
variable/ that's the problem.
So it should
be wrong. I think I am missing the knowledge about string literal . Is
it handled specially than local variable .
It's handled /differently/. It's not a local variable.
Suppose
int x = 5;
return &x;
According to me we should not this, as address of x will be scrapped
after the execution flow returns from particular function.
Yes. But that's not the same thing at all.

--
owl:differentFr om Hedgehog
Meaning precedes definition.

Dec 14 '07 #15
CBFalconer wrote:
>
pete wrote:
In C, the type of ("Hello") is (char [6]), not (const char [6]).

Not when it has been told otherwise.
The C standard says that the array elements have type char.
You don't get to tell C otherwise.

Maybe there's another newsgroup
where your language extensions
that allow you to respecify parts of C
that have been already specified by the C standard,
are on topic, but this isn't it.

--
pete
Dec 14 '07 #16
"pete" <pf*****@mindsp ring.comwrote in message
news:47******** ***@mindspring. com...
CBFalconer wrote:
>pete wrote:
In C, the type of ("Hello") is (char [6]), not (const char [6]).

Not when it has been told otherwise.

The C standard says that the array elements have type char.
You don't get to tell C otherwise.
-Wwrite-strings tells GCC to act _as if_ string literals were of type const
char[] and warn accordingly, though programs still compile correctly. Since
compilers are allowed to emit any diagnostics they want, even incorrect
ones, it's still compliant (as far as this goes, at least). This is little
different from the canonical example of warning on "if (x=0)".

This is a pretty decent warning to have around if you're starting a project
from scratch. Literals _should_ have been const char[] in C90, but they
were left as merely char[] due to the massive existing codebase that assumed
they weren't const.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Dec 14 '07 #17
On Fri, 14 Dec 2007 12:40:46 -0600, Stephen Sprunk wrote:
"pete" <pf*****@mindsp ring.comwrote in message
news:47******** ***@mindspring. com...
>CBFalconer wrote:
>>pete wrote:
In C, the type of ("Hello") is (char [6]), not (const char [6]).

Not when it has been told otherwise.

The C standard says that the array elements have type char. You don't
get to tell C otherwise.

-Wwrite-strings tells GCC to act _as if_ string literals were of type
const char[] and warn accordingly, though programs still compile
correctly. Since compilers are allowed to emit any diagnostics they
want, even incorrect ones, it's still compliant (as far as this goes, at
least). This is little different from the canonical example of warning
on "if (x=0)".
-Wwrite-strings causes this strictly conforming program to be rejected by
design:

int main(void) {
if (0) *"" = 'x';
}

Because of this, -Wwrite-strings, while very useful, causes gcc to not
act as a conforming implementation.
(However, even without -Wwrite-strings, gcc has a bug in which the
equally strictly conforming

int main(void) {
if (0) ""[0] = 'x';
}

is unconditionally rejected.)
Dec 14 '07 #18
Harald van D?k wrote:
>
.... snip ...
>
(However, even without -Wwrite-strings, gcc has a bug in which the
equally strictly conforming

int main(void) {
if (0) ""[0] = 'x';
}

is unconditionally rejected.)
Not a bug. "" is a constant string, stored in (possibly) constant
memory, and thus is not writable. You may be complaining that gcc
hasn't bother to notice that the statment won't be executed, and
thus should suppress the message. However, compilers are allowed
to emit all the messages they wish.

--
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 #19
somenath wrote:
CBFalconer <cbfalco...@yah oo.comwrote:
>somenath wrote:
>>#include<stdi o.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.

Just to clarify myself I would like to explain my understanding.
In the definition char *abc ="Hello";
Say 'H' is stored in address 100 and the 'e' will be stored in 108 and
so on . Now "abc" will have the value 100. When "return abc" is
getting executed it will try to return 100 . Is it not correct? If it
is so I am trying to return a address obtained locally. So it should
be wrong. I think I am missing the knowledge about string literal . Is
it handled specially than local variable .
No, because in your case above the 100 is an address in static
memory, and has nothing to do with the automatic memory of the
function. abc is in that automatic memory, and has been set to
100. The accessibility and content of 100 does not change on
exiting the function.

--
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 #20

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

Similar topics

0
1845
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
4122
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
34588
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
16355
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
5986
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
1871
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
9688
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...
1
10247
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
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
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
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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
4146
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
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.