473,761 Members | 8,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return discards qualifiers from pointer target type

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
target type" when I *access* a member of an argument defined as const?

Please see the code below:

/*** START TEST.C ***/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_NAME_SIZE 80
#define MY_NAME "This is my name"

struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};

void set_mys_name(st ruct my_s *, const char *);
char *get_mys_name(c onst struct my_s *);

int main(void) {
struct my_s *mysp;

mysp = malloc(sizeof *mysp);

set_mys_name(my sp, MY_NAME);

printf("My name is %s\n", get_mys_name(my sp));

return (0);
}

void
set_mys_name(st ruct my_s *mys, const char *name)
{
if(!mys) return;
strncpy(mys->mys_name, name, MAX_NAME_SIZE);
}

char *
get_mys_name(co nst struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
/*** START TEST.C ***/
gcc -Wall -o test test.c
test.c: In function `get_mys_name':
test.c:39: warning: return discards qualifiers from pointer target type
./test
My name is This is my name
Thank you!
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #1
17 12786
Pietro Cerutti <ga**@gahr.chwr ites:
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
target type" when I *access* a member of an argument defined as const?
<snip>
struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
....
char *
get_mys_name(co nst struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.

--
Ben.
Aug 28 '07 #2
Ben Bacarisse wrote:
Pietro Cerutti <ga**@gahr.chwr ites:
>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
target type" when I *access* a member of an argument defined as const?

<snip>
>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
...
>char *
get_mys_name(c onst struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}

The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.
So, so to speak, when a struct is defined as const, each pointer trying
to reference to a member of that struct using the "->" operator has to
be const as well.
Am I correct?

Thank you for your input!
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #3
Ben Bacarisse wrote:
Pietro Cerutti <ga**@gahr.chwr ites:
>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
target type" when I *access* a member of an argument defined as const?

<snip>
>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
...
>char *
get_mys_name(c onst struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}

The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.
Sorry for double posting:
I though that a const in a parameter only meant that the function itself
isn't going to modify the parameter. The object itself (the struct)
hasn't been defined as const, so a function calling get_mys_name() has
the right to modify the name using the pointer returned by the function.
What's wrong with it?
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #4
Pietro Cerutti wrote:
>
.... snip ...
>
I though that a const in a parameter only meant that the function
itself isn't going to modify the parameter. The object itself (the
struct) hasn't been defined as const, so a function calling
get_mys_name() has the right to modify the name using the pointer
returned by the function. What's wrong with it?
Because you aren't returning the component of the original. You
are returning a component of the functional parameter, which you
promised not to disturb.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

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

Aug 28 '07 #5
Pietro Cerutti wrote:
>
.... snip ...
>
Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?
Because the struct is const. You can't have a const struct without
having all the components automatically being const.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

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

Aug 28 '07 #6
Pietro Cerutti <ga**@gahr.chwr ites:
Ben Bacarisse wrote:
>Pietro Cerutti <ga**@gahr.chwr ites:
<snip>
>>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
...
>>char *
get_mys_name( const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}

The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.

So, so to speak, when a struct is defined as const, each pointer trying
to reference to a member of that struct using the "->" operator has to
be const as well.
Am I correct?
Yes. Your language is a little vague but that is the gist of it.
Section 6.5.2.3 paragraphs 3 and 4 if you want chapter and verse.

When you access a struct member using either . or ->, the expression
takes on the type of the member, qualified in the same way as the
struct object (const is a type qualifier).

--
Ben.
Aug 28 '07 #7
Ben Bacarisse wrote:
Pietro Cerutti <ga**@gahr.chwr ites:
>Ben Bacarisse wrote:
>>Pietro Cerutti <ga**@gahr.chwr ites:
<snip>
>>>struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
...
char *
get_mys_name (const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.
So, so to speak, when a struct is defined as const, each pointer trying
to reference to a member of that struct using the "->" operator has to
be const as well.
Am I correct?

Yes. Your language is a little vague but that is the gist of it.
Section 6.5.2.3 paragraphs 3 and 4 if you want chapter and verse.
English's not my mother language..
>
When you access a struct member using either . or ->, the expression
takes on the type of the member, qualified in the same way as the
struct object (const is a type qualifier).
Clear, thank you!
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #8
Pietro Cerutti <ga**@gahr.chwr ites:
Ben Bacarisse wrote:
>Pietro Cerutti <ga**@gahr.chwr ites:
<snip>
>>char *
get_mys_name( const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}

The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.

Sorry for double posting:
I though that a const in a parameter only meant that the function itself
isn't going to modify the parameter. The object itself (the struct)
hasn't been defined as const, so a function calling get_mys_name() has
the right to modify the name using the pointer returned by the function.
What's wrong with it?
You are little confused about what is const. Your function declares
mys as a pointer to a constant my_s structure. The parameter itself,
mys, is not at all constant. The function can do 'mys += 1;' if it
likes -- it can't do 'mys->mys_name[0] = 'a';' because the whole
structure pointer to by mys is constant.

A few examples:

struct s x; /* x is a struct s */
const struct s y; /* y is a constant struct s */
struct s const w; /* ditto, but some find this one odd to read! */
struct s *const p; /* p is constant pointer to a (non-const) struct */
const struct s *q; /* q is a pointer to a constant struct s */
const struct s *const r; /* both p and the struct it points to are const */

Does that help at all?

--
Ben.
Aug 28 '07 #9
Ben Bacarisse wrote:
Pietro Cerutti <ga**@gahr.chwr ites:
>Ben Bacarisse wrote:
>>Pietro Cerutti <ga**@gahr.chwr ites:
<snip>
>>>char *
get_mys_name (const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
The object pointed to by mys is const. This function returns a plain
(non-const) pointer to part of it (the mys_name array). The caller
could modify the contents of this array through this ponter. The
compiler is warning you that this function breaks the constness of the
object its parameter points to.
Sorry for double posting:
I though that a const in a parameter only meant that the function itself
isn't going to modify the parameter. The object itself (the struct)
hasn't been defined as const, so a function calling get_mys_name() has
the right to modify the name using the pointer returned by the function.
What's wrong with it?

You are little confused about what is const. Your function declares
mys as a pointer to a constant my_s structure. The parameter itself,
mys, is not at all constant. The function can do 'mys += 1;' if it
likes -- it can't do 'mys->mys_name[0] = 'a';' because the whole
structure pointer to by mys is constant.

A few examples:

struct s x; /* x is a struct s */
const struct s y; /* y is a constant struct s */
struct s const w; /* ditto, but some find this one odd to read! */
struct s *const p; /* p is constant pointer to a (non-const) struct */
const struct s *q; /* q is a pointer to a constant struct s */
const struct s *const r; /* both p and the struct it points to are const */

Does that help at all?
No, /*that*/ was clear. I do understand the the pointer is not const,
the structure is, and therefore you haven't got the right to modify its
members.

Now I understood that you can't return members of the structure without
declaring them const as well.

What I still don't understand is why: if the structure is const only
inside the function receiving it as a const parameter (thus not const at
the caller) the function should be able to return to the caller members
of the struct not declared as const, since the caller has the right to
modify them.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #10

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

Similar topics

0
2682
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code: class Stream: _public_methods_ =
6
95850
by: Jason | last post by:
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;
12
4888
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 -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
8
2595
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
2
2972
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
4
2541
by: rohitsagar | last post by:
I want to do fork from a program Code is very simple, below is the code, it just execute a executable called a.exe, I want to run a.exe 600 times. #include<stdio.h> #include<stdlib.h> void main() { pid_t pid;
2
2257
by: Thelma Lubkin | last post by:
I use my own matrix and vector classes. I wrote them before such things were generally available and I've stuck with them ever since. I've just added an Octonion class derived from the vectors class and I'm trying to do some matrix arithmetic where the elements of the matrices are Octonions. (My Octonions are essentially 8-component vectors with a multiplication table that I compute at runtime. Like vectors, they are a template class, but...
4
8918
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 code. What's wrong with these lines? I searched the web for it, but didn't find anything useful. Thanks,
49
2732
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
9554
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
10136
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
9988
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
7358
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
6640
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
5266
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
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
3
3509
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.