473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Warning meaning.

mdh
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[1], "-n") == 0) /* argv is "-n" /
******/
.......}

int mystrcomp( char *s, char *t){....}

I get the following warning related to the line marked by asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."

I am sure I will have some more questions, but what exactly does this
warning mean?

thanks, in advance.

Jun 22 '07 #1
22 1792
mdh said:
Hi All,
Happy Solstice!
<grump>
We do April Fool's Day, and that's it. No solstices, no ramadans, no
christmases, no divalis, no nuffink. Just 1 April (or April 1, on some
planets).
</grump>
May I ask the following.

The following is a brief excerpt of a practice program.

main(...){
if (argc 1 && mystrcomp(argv[1], "-n") == 0) /* argv is "-n" /
******/
......}

int mystrcomp( char *s, char *t){....}

I get the following warning related to the line marked by asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."

I am sure I will have some more questions, but what exactly does this
warning mean?
It means your compiler is lying to you, by pretending that string
literals are const, rather than merely constant.

Nevertheless, if all you're doing is comparing strings, it will do you
no harm to make those pointers const:

int mystrcomp(const char *s, const char *t)

and as a pleasant side-effect, that spurious warning should vanish into
the mist.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #2
mdh <md**@comcast.netwrites:
The following is a brief excerpt of a practice program.

main(...){
if (argc 1 && mystrcomp(argv[1], "-n") == 0) /* argv is "-n" /
******/
......}

int mystrcomp( char *s, char *t){....}

I get the following warning related to the line marked by asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."

I am sure I will have some more questions, but what exactly does this
warning mean?
This is a very good illustration of why we ask people to post *exact*
code and error messages (copy-and-paste, don't re-type).

Your error message refers to a function "strcomp", but your code
declares and calls a function called "mystrcomp". I could guess that
it's just a typo, but if you had copy-and-pasted your actual code I
wouldn't have to guess.

Assuming that "strcomp" is really "mystrcomp", your compiler is
complaining about the first argument, which is argv[1]. Assuming that
argv is the usual parameter to main (we can't be certain of that
either), you have to declare it yourself -- right where you've written
"main(...)".

Is a declaration for your mystrcomp() function visible at the point
where you've called it?

Reduce your program to a smaller *complete* version that exhibits the
same problem. Give us something we can try ourselves.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 22 '07 #3
mdh
On Jun 22, 1:36 am, Richard Heathfield <r...@see.sig.invalidwrote:
<grump>
>........
</grump>
Sorry...no offense meant.

main(...){
if (argc 1 && mystrcomp(argv[1], "-n") == 0) /* argv is "-n" /
******/
......}
int mystrcomp( char *s, char *t){....}
I get the following warning related to the line marked by asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."
It means your compiler is lying to you, by pretending that string
literals are const, rather than merely constant.

Richard...from our previous discussions, I thought a string literal
like "hello world" was a constant? Could you explain a little further
what you mean by "literals are const, rather than merely constant."

>
Nevertheless, if all you're doing is comparing strings, it will do you
no harm to make those pointers const:

int mystrcomp(const char *s, const char *t)

and as a pleasant side-effect, that spurious warning should vanish into
the mist.
.....which is exactly what happened.

Jun 22 '07 #4
mdh
On Jun 22, 1:41 am, Keith Thompson <k...@mib.orgwrote:
mdh <m...@comcast.netwrites:
The following is a brief excerpt of a practice program.
main(...){
if (argc 1 && mystrcomp(argv[1], "-n") == 0) /* argv is "-n" /
******/
......}
int mystrcomp( char *s, char *t){....}


This is a very good illustration of why we ask people to post *exact*
code and error messages (copy-and-paste, don't re-type).

Your error message refers to a function "strcomp", but your code
declares and calls a function called "mystrcomp". I could guess that
it's just a typo,
Indeed it was...apologies..it is rather late here and I am on
call...so not at my best.
Reduce your program to a smaller *complete* version that exhibits the
same problem. Give us something we can try ourselves.
Ok...I was trying to navigate the road between " just the program
please, not all the extraneous stuff " and "not enough" ...but I get
your point. :-) Next time I will be clearer.

Jun 22 '07 #5
mdh
Sorry, Keith. I am quite novel at this..but with all the polishing I
am getting from the clc, I am sure I will start to shine in a few
years!!!!
I get the following warning related to the line marked by asterisks.
..........snip......
>
This is a very good illustration of why we ask people to post *exact*
code and error messages (copy-and-paste, don't re-type).
Is a declaration for your mystrcomp() function visible at the point
where you've called it?
I believe so...see below.
>
Reduce your program to a smaller *complete* version that exhibits the
same problem. Give us something we can try ourselves.

-
#include <stdio.h>
#define MAXLINES 10
int strcomp( char *, char *);

int main (int argc, const char * argv[]) {
int numerics=0;
if (argc 1 && strcomp(argv[1], "-n") == 0)
numerics=1;
/* warning: passing argument 1 of 'strcomp' discards qualifiers from
pointer target type*/

return 0;
}
int strcomp( char *s, char *t){
for ( ; *s == *t; s++, t++)
if ( *s == '\0')
return 0;
return *s - *t;
}

Jun 22 '07 #6
mdh wrote:
>
int main (int argc, const char * argv[])
There's your problem. Should be:

int main(int argc, char *argv[])

instead.

--
pete
Jun 22 '07 #7
mdh said:
On Jun 22, 1:36 am, Richard Heathfield <r...@see.sig.invalidwrote:
<grump>
>>........
</grump>

Sorry...no offense meant.
Oh, don't worry - I'm not offended, just grumpy. :-)

I get the following warning related to the line marked by
asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."
It means your compiler is lying to you, by pretending that string
literals are const, rather than merely constant.


Richard...from our previous discussions, I thought a string literal
like "hello world" was a constant?
Yes. It is an array of 12 char, with static storage duration, and we're
not *allowed* to write to it, which is why we call it constant, but it
is *not* const. An analogy for you: we're not *allowed* to mug little
old ladies in the street. But if we *do*, we'll probably get away with
it, more's the pity. Nevertheless, we still ought not to mug little old
ladies, and we still ought not write to string literals. And just as we
*might* get caught by the police, so we *might* have our program
explode in our faces because the implementation wrote the string
literal into read-only memory or because it merged duplicate string
literals.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #8
mdh said:

<snip>
int main (int argc, const char * argv[]) {
See pete's comment elsethread.
int strcomp( char *s, char *t){
strcomp invades implementation namespace. These alternatives do not:

mystrcomp
str_comp
compstr

Furthermore, const char *s and const char *t would be an improvement, as
I believe you have already discovered (possibly after posting the
above).
for ( ; *s == *t; s++, t++)
if ( *s == '\0')
return 0;
return *s - *t;
In general terms, it's more robust to do this in terms of relational
expressions rather than arithmetic. For example:

return (*s *t) - (*s < *t);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #9

"mdh" <md**@comcast.netha scritto nel messaggio news:11**********************@m37g2000prh.googlegr oups.com...
Sorry, Keith. I am quite novel at this..but with all the polishing I
am getting from the clc, I am sure I will start to shine in a few
years!!!!
I get the following warning related to the line marked by asterisks.
.........snip......
>>
This is a very good illustration of why we ask people to post *exact*
code and error messages (copy-and-paste, don't re-type).

>Is a declaration for your mystrcomp() function visible at the point
where you've called it?

I believe so...see below.
>>
Reduce your program to a smaller *complete* version that exhibits the
same problem. Give us something we can try ourselves.

-

#include <stdio.h>
#define MAXLINES 10
int strcomp( char *, char *);

int main (int argc, const char * argv[]) {
int numerics=0;
if (argc 1 && strcomp(argv[1], "-n") == 0)
numerics=1;
/* warning: passing argument 1 of 'strcomp' discards qualifiers from
pointer target type*/
Passing argv[1] which is a const char* to strcomp which wants a
char* discards the const qualifier. How could the warning be
clearer?
(And I'm not sure if the second argument of main can be const, but
I feel too lazy to check the standard right now, so for now I
suggest to declare it without const.)
Jun 22 '07 #10
mdh
On Jun 22, 3:29 am, Richard Heathfield <r...@see.sig.invalidwrote:
Richard...from our previous discussions, I thought a string literal
like "hello world" was a constant?

Yes. It is an array of 12 char, with static storage duration, and we're
not *allowed* to write to it, which is why we call it constant, but it
is *not* const.
Just out of curiosity, is this memory actually in some area which is
designated "read-only" or is it any old memory which the compiler
decides is to be read-only until it is made read/write again?
............ wrote the string
literal into read-only memory or because it merged duplicate string
literals.
So, if I understand you correctly, in the function definition
( understanding that it should have different name) the term "constant
char * implies that the pointer (to the char array) being passed
should point to a static area of memory?

Jun 22 '07 #11
mdh
On Jun 22, 3:11 am, pete <pfil...@mindspring.comwrote:
mdh wrote:
int main (int argc, const char * argv[])

There's your problem. Should be:

int main(int argc, char *argv[])

instead.

--
pete
Thank you Pete..
I am beginning to understand the difference in terms of "type of
memory"..(.as per RH.) ..for a char array.

Jun 22 '07 #12
mdh
On Jun 22, 3:39 am, Richard Heathfield <r...@see.sig.invalidwrote:
>
for ( ; *s == *t; s++, t++)
if ( *s == '\0')
return 0;
return *s - *t;

In general terms, it's more robust to do this in terms of relational
expressions rather than arithmetic. For example:

return (*s *t) - (*s < *t);

Thanks ...never seen it that way...but it is quite elegant.

Jun 22 '07 #13
mdh <md**@comcast.netwrote:
On Jun 22, 3:29 am, Richard Heathfield <r...@see.sig.invalidwrote:
Richard...from our previous discussions, I thought a string literal
like "hello world" was a constant?
Yes. It is an array of 12 char, with static storage duration, and we're
not *allowed* to write to it, which is why we call it constant, but it
is *not* const.

Just out of curiosity, is this memory actually in some area which is
designated "read-only" or is it any old memory which the compiler
decides is to be read-only until it is made read/write again?
Depends entirely on the implementation. On an embedded device it may
well be cost-effective to burn string literals into ROM. Some compilers
for home computers may not even bother actually making it read-only.

Richard
Jun 22 '07 #14
mdh said:
On Jun 22, 3:29 am, Richard Heathfield <r...@see.sig.invalidwrote:
Richard...from our previous discussions, I thought a string literal
like "hello world" was a constant?

Yes. It is an array of 12 char, with static storage duration, and
we're not *allowed* to write to it, which is why we call it constant,
but it is *not* const.

Just out of curiosity, is this memory actually in some area which is
designated "read-only" or is it any old memory which the compiler
decides is to be read-only until it is made read/write again?
It depends on the implementation. There's nothing to stop an
implementation from writing read-only data to an EPROM, for example.
>............ wrote the string
literal into read-only memory or because it merged duplicate string
literals.

So, if I understand you correctly, in the function definition
( understanding that it should have different name) the term "constant
char * implies that the pointer (to the char array) being passed
should point to a static area of memory?
No, I don't know where you got that from. All I'm saying is "don't
modify string literals". The const char * is a promise that the
function won't write through that pointer - i.e. won't change the
string that is passed to it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #15
Richard Heathfield wrote:
mdh said:
>>
The following is a brief excerpt of a practice program.

main(...){
if (argc 1 && mystrcomp(argv[1], "-n") == 0) /* argv is "-n" /
******/
......}

int mystrcomp( char *s, char *t){....}

I get the following warning related to the line marked by asterisks.
"Warning:passing argument 1 of 'strcomp' discards qualifiers from
pointer target type."

I am sure I will have some more questions, but what exactly does this
warning mean?

It means your compiler is lying to you, by pretending that string
literals are const, rather than merely constant.
Hard to be sure, but I don't think that's the issue.
The message refers to argument 1, argv[1], not to the
string literal that is argument 2. (Of course, the message
also refers to strcomp and not to mystrcomp, so we know
the facts have been fudged a little ...)

It would be helpful to see the whole program, or at
least the true nature of `...' in `main(...)'.
Nevertheless, if all you're doing is comparing strings, it will do you
no harm to make those pointers const:

int mystrcomp(const char *s, const char *t)
Yes, that's a good idea in any case.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 22 '07 #16
mdh
On Jun 22, 5:31 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
On Jun 22, 3:29 am, Richard Heathfield <r...@see.sig.invalidwrote:
Yes. It is an array of 12 char, with static storage duration, and we're
not *allowed* to write to it, which is why we call it constant, but it
is *not* const.
Just out of curiosity, is this memory actually in some area which is
designated "read-only" or is it any old memory which the compiler
decides is to be read-only until it is made read/write again?

Depends entirely on the implementation.
thanks

Jun 22 '07 #17
mdh
On Jun 22, 5:38 am, Richard Heathfield <r...@see.sig.invalidwrote:
mdh said:
No, I don't know where you got that from. All I'm saying is "don't
modify string literals". The const char * is a promise that the
function won't write through that pointer - i.e. won't change the
string that is passed to it.
Ok...I now I see what you mean. Thank you.
Jun 22 '07 #18
mdh wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
.... snip ...
>
>return (*s *t) - (*s < *t);

Thanks ...never seen it that way...but it is quite elegant.
Since it never deals with numbers greater than 1, it avoids all
possibilities of overflow. Also works for all numerical types for
*s and *t.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

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

Jun 22 '07 #19
Richard Bos wrote:
mdh <md**@comcast.netwrote:
.... snip ...
>
>Just out of curiosity, is this memory actually in some area which
is designated "read-only" or is it any old memory which the
compiler decides is to be read-only until it is made read/write
again?

Depends entirely on the implementation. On an embedded device it
may well be cost-effective to burn string literals into ROM. Some
compilers for home computers may not even bother actually making
it read-only.
[OT] If you are using gcc, try:

"-W -Wall -ansi -pedantic -Wwrite-strings"

as flags to catch things early. [/OT]

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

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

Jun 22 '07 #20
mdh
Richard Heathfield wrote:
>
return (*s *t) - (*s < *t);
....snip...

On Jun 22, 8:03 am, CBFalconer < wrote:
Since it never deals with numbers greater than 1, it avoids all
possibilities of overflow. Also works for all numerical types for
*s and *t.

thanks Chuck...that's what I like about it.

Jun 22 '07 #21
On Fri, 22 Jun 2007 05:20:40 -0700, mdh <md**@comcast.netwrote:
>On Jun 22, 3:11 am, pete <pfil...@mindspring.comwrote:
>mdh wrote:
int main (int argc, const char * argv[])

There's your problem. Should be:

int main(int argc, char *argv[])

instead.

--
pete

Thank you Pete..
I am beginning to understand the difference in terms of "type of
memory"..(.as per RH.) ..for a char array.
Perhaps you are but it has nothing to do with the problem at hand. In
you declaration of main, you stated that argv "could be considered as"
an array of pointers (we know it is actually a char**) and that each
pointer in the array would point to a constant char. You then passed
argv[1] to a function as an argument that did not agree to treat the
character pointed to as constant. This is what your compiler is
complaining about. The const qualifier that applies to *argv[1] does
not apply to the object pointed to by parameter s of your function.
This would allow the function to legally alter *s but you said
*argv[1] (which is the same object) cannot be modified. The compiler
is pointing out the inconsistency.

If you had cast the argument to an unqualified char*, the compiler
would not complain (actually the compiler can issue any diagnostic it
feels like but that is a different issue). It is only if your
function then attempted to modify *s that the type of memory would be
significant. If the character were actually stored in read only
memory, your hardware/operating system would prevent you from
completing the modification. If not, chances are the modification
would succeed but your code would have entered the realm of undefined
behavior.
Remove del for email
Jun 22 '07 #22
mdh
On Jun 22, 4:06 pm, Barry Schwarz <schwa...@doezl.netwrote:
mdh wrote:
>
Thank you Pete..
I am beginning to understand the difference in terms of "type of
memory"..(.as per RH.) ..for a char array.


In you declaration of main, you stated that argv "could be considered as"
an array of pointers (we know it is actually a char**) and that each
pointer in the array would point to a constant char. You then passed
argv[1] to a function as an argument that did not agree to treat the
character pointed to as constant. This is what your compiler is
complaining about. The const qualifier that applies to *argv[1] does
not apply to the object pointed to by parameter s of your function.
This would allow the function to legally alter *s but you said
*argv[1] (which is the same object) cannot be modified. The compiler
is pointing out the inconsistency.

If you had cast the argument to an unqualified char*, the compiler
would not complain .................snip............ It is only if your
function then attempted to modify *s that the type of memory would be
significant.

Thank you Barry.. I believe I see what is being emphasized.
Jun 23 '07 #23

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

Similar topics

4
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",...
3
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 ...
32
by: Stephen | last post by:
Is there a standard way to remove the warning that a C compiler might produce from the statement: if (a = b) {} I don't want to do: if ((a = b) != 0) {} Because my "a = b" is actually...
5
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!
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: Sid Price | last post by:
Does VB.NET have any support for inline warning suppression like C#? I am porting a project from VB.NET 2003 to VB.NET 2005 and I have a bunch of warnings that after careful examination are benign....
20
by: somenath | last post by:
Hi All, I have one question regarding the code. #include<stdio.h> char *f1(void); char *f1(void) { char *abc ="Hello";
20
by: jacob navia | last post by:
Consider this code static typedef struct { int boo; } FOO; This provokes with MSVC: ------------------------------ Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64...
11
by: Jeff | last post by:
I turned on errors in php: ini_set('display_errors','1'); And I got a slew of notices and a couple of warnings. The notices are mostly missing indexes from doing things like this: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.