473,395 Members | 1,689 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,395 software developers and data experts.

warning: assignment discards qualifiers from pointer target type

I have a function (Inet_ntop) that returns const char * and if I try to
assign that return value to a char * variable, I get the gcc error message:
warning: assignment discards qualifiers from pointer target type

Does anyone know what this warning means? Why do I get it? The program
compiles and appears to work, but I'd like to understand the warning.

Here is basically what the code looks like:

char *str;
str = Inet_ntop(...); //returns const char *

Any help is appreciated. Thanks.

--
Jason
[ jake1138 AT yahoo DOT com ]
Nov 13 '05 #1
6 95778
"Jason" <ja******@NO.SPAM.yahoo.com> wrote in message
news:bm**********@terabinaries.xmission.com...
I have a function (Inet_ntop) that returns const char * and if I try to
assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from pointer target type

Does anyone know what this warning means?
It means you're walking on eggs. :-)
Why do I get it?
You have a good compiler.
The program
compiles and appears to work, but I'd like to understand the warning.
The moment you try to modify what the assigned-to pointer
points to, you could possibly induce undefined behavior.

Here is basically what the code looks like:

char *str;
str = Inet_ntop(...); //returns const char *
It's warning you that the "protection" of the pointer's
target provided by the 'const' qualifier is lost when
the 'const' is thrown away by assigning it to a "plain"
type 'char*' pointer.

It's warning you that you're boating in deep water,
and you've thrown your life preserver overboard.

const char array[]="whatever";
array[0] = 'X'; /* compiler must tell you, "Can't do that!" */

const char *cp = array;
cp[0] = 'X'; /* compiler must tell you, "Can't do that!" */
char *p = array; /* valid, but many compilers will warn you:
"Danger, Will Robinson!" */

/* because... */

p[0]; /* is valid, although possibly/probably not what you
really wanted, and has possiblity of undefined behavior */

Any help is appreciated. Thanks.


Write:

const char *str;
str = Inet_ntop(...); //returns const char *
The function returns type 'const char*' for a reason.
It's telling you "You can look, but don't touch the
target of the returned pointer."

HTH,
-Mike
Nov 13 '05 #2
"Jason" <ja******@NO.SPAM.yahoo.com> wrote in message
news:bm**********@terabinaries.xmission.com...
I have a function (Inet_ntop) that returns const char * and if I try to
assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from pointer target type

Does anyone know what this warning means?
It means you're walking on eggs. :-)
Why do I get it?
You have a good compiler.
The program
compiles and appears to work, but I'd like to understand the warning.
The moment you try to modify what the assigned-to pointer
points to, you could possibly induce undefined behavior.

Here is basically what the code looks like:

char *str;
str = Inet_ntop(...); //returns const char *
It's warning you that the "protection" of the pointer's
target provided by the 'const' qualifier is lost when
the 'const' is thrown away by assigning it to a "plain"
type 'char*' pointer.

It's warning you that you're boating in deep water,
and you've thrown your life preserver overboard.

const char array[]="whatever";
array[0] = 'X'; /* compiler must tell you, "Can't do that!" */

const char *cp = array;
cp[0] = 'X'; /* compiler must tell you, "Can't do that!" */
char *p = array; /* valid, but many compilers will warn you:
"Danger, Will Robinson!" */

/* because... */

p[0]; /* is valid, although possibly/probably not what you
really wanted, and has possiblity of undefined behavior */

Any help is appreciated. Thanks.


Write:

const char *str;
str = Inet_ntop(...); //returns const char *
The function returns type 'const char*' for a reason.
It's telling you "You can look, but don't touch the
target of the returned pointer." If you ignore this
warning, you're on your own.

HTH,
-Mike

Nov 13 '05 #3
On 2003-10-08, Jason <ja******@NO.SPAM.yahoo.com> wrote:

char *str;
str = Inet_ntop(...); //returns const char *


You should declare str variable to be the same type as that being
returned by Inet_ntop().

const char *str = Inet_ntop(/* ... */);

The "const" is a qualifier. It means the return value of Inet_ntop
is a pointer to data that should not be modified.

However, in the erroneous code, you assigned that pointer to a
regular "char *", which discards the "should not be modified"
qualifier.

-- James
Nov 13 '05 #4
Thanks Mike and James for your answers! I think I was confused as to how
const worked with pointers in that way, but now I understand.

--
Jason
[ jake1138 AT yahoo DOT com ]
Nov 13 '05 #5

"Jason" <ja******@NO.SPAM.yahoo.com> wrote in message
news:bm*********@terabinaries.xmission.com...
Thanks Mike and James for your answers! I think I was confused as to how
const worked with pointers in that way, but now I understand.


It can get confusing. :-)

/* (0) */ int * pi; /* pointer to int */
/* (can modify p or *p) */

/* (1) */ int const * pci; /* pointer to const int */
/* (can modify p, but not *p) */

/* (2) */ const int * pci2; /* same as (1) */

/* (3) */ int * const cpi; /* const pointer to int */
/* (can modify *p, but not p */

/* (4) */ int const * const cpci; /* const pointer to const int */
/* (cannot modify p nor *p) */

/* (5) */ const int * const cpci2; /* same as (4) */
HTH,
-Mike
Nov 13 '05 #6
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Z_*****************@newsread4.news.pas.earthl ink.net...
/* (4) */ int const * const cpci; /* const pointer to const int */
/* (cannot modify p nor *p) */

/* (5) */ const int * const cpci2; /* same as (4) */


Dang, that's what I get for 'copy-n-pasting' :-)

Of course the 'p' and '*p' in the comments refer to
the actual identifier in each declaration
('pci', 'cpi', etc.)

Sorry about that.

-Mike
Nov 13 '05 #7

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

Similar topics

12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
29
by: Daniel Rudy | last post by:
Hello, Consider the following code fragment: fileptr = fopen(param->filename, "r"); if (fileptr == NULL) { error("digest.c: digest_file: Open File ", errno); return(-2); }
22
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" / ******/...
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
4
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
3
by: lovecreatesbea... | last post by:
I'm getting `warning: return discards qualifiers from pointer target type' at line 11 on this code from gcc. Some people suggested in old posts that this kind of warning can be suppressed by...
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";
4
by: Andre | last post by:
Hi All, When I compile the following piece of code with gcc, I get 3 "warning: initialization discards qualifiers from pointer target type" messages which refer to the 3 lines marked in the...
33
by: James H. Newman | last post by:
I have a portion of code along the following lines: volatile unsigned char x ; unsigned int f(unsigned char *y) ; When I do unsigned int z = f(&x) ;
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.