return discards qualifiers from pointer target type

Discussion in 'C Programming' started by Pietro Cerutti, Aug 28, 2007.

  1. 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(struct my_s *, const char *);
    char *get_mys_name(const struct my_s *);

    int main(void) {
    struct my_s *mysp;

    mysp = malloc(sizeof *mysp);

    set_mys_name(mysp, MY_NAME);

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

    return (0);
    }

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

    char *
    get_mys_name(const 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
     
    Pietro Cerutti, Aug 28, 2007
    #1
    1. Advertising

  2. Pietro Cerutti <> writes:

    > 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(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.

    --
    Ben.
     
    Ben Bacarisse, Aug 28, 2007
    #2
    1. Advertising

  3. Ben Bacarisse wrote:
    > Pietro Cerutti <> writes:
    >
    >> 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(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?

    Thank you for your input!


    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #3
  4. Ben Bacarisse wrote:
    > Pietro Cerutti <> writes:
    >
    >> 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(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?


    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #4
  5. Pietro Cerutti

    CBFalconer Guest

    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
     
    CBFalconer, Aug 28, 2007
    #5
  6. Pietro Cerutti

    CBFalconer Guest

    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
     
    CBFalconer, Aug 28, 2007
    #6
  7. Pietro Cerutti <> writes:

    > Ben Bacarisse wrote:
    >> Pietro Cerutti <> writes:
    >>

    <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.
     
    Ben Bacarisse, Aug 28, 2007
    #7
  8. Ben Bacarisse wrote:
    > Pietro Cerutti <> writes:
    >
    >> Ben Bacarisse wrote:
    >>> Pietro Cerutti <> writes:
    >>>

    > <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
     
    Pietro Cerutti, Aug 28, 2007
    #8
  9. Pietro Cerutti <> writes:

    > Ben Bacarisse wrote:
    >> Pietro Cerutti <> writes:

    <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.
     
    Ben Bacarisse, Aug 28, 2007
    #9
  10. Ben Bacarisse wrote:
    > Pietro Cerutti <> writes:
    >
    >> Ben Bacarisse wrote:
    >>> Pietro Cerutti <> writes:

    > <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
     
    Pietro Cerutti, Aug 28, 2007
    #10
  11. Pietro Cerutti <> writes:

    > Ben Bacarisse wrote:

    <snip>
    >> 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.


    OK, but just so you see why I thought you had that confused, here is
    what you said:

    | I though that a const in a parameter only meant that the function itself
    | isn't going to modify the parameter.

    "modify the parameter" is confusing here. Strictly speaking, the
    parameter is mys and not the thing mys points to. mys can be modified
    by this function because it is not declared as being const. I get
    that you get that, but it was not at all clear from the above!

    > 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.


    The reason is that the compiler can't know and thus needs a general
    rule:

    struct s { char name[100]; };

    char *open_up(const struct s *sp)
    {
    return sp->name; /* example code: this is an error! */
    }

    void safe(void)
    {
    struct s data;
    open_up(&data)[0] = 'a';
    }

    void unsafe(void)
    {
    const struct s data;
    open_up(&data)[0] = 'a';
    }

    You are right that the function 'open_up' does not allow 'safe' to do
    anything that it can't already do, but 'usafe' should be stopped. How
    can the compiler tell the difference? All three can be compiled
    separately, of course.

    When an object is declared const, it does not just mean that you won't
    change it. It also means that you won't hand out the means by which
    it may be changed (not at least with out a diagnostic message).

    --
    Ben.
     
    Ben Bacarisse, Aug 28, 2007
    #11
  12. Ben Bacarisse wrote:
    > Pietro Cerutti <> writes:
    >
    >> Ben Bacarisse wrote:

    > <snip>
    >>> 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.

    >
    > OK, but just so you see why I thought you had that confused, here is
    > what you said:
    >
    > | I though that a const in a parameter only meant that the function itself
    > | isn't going to modify the parameter.
    >
    > "modify the parameter" is confusing here. Strictly speaking, the
    > parameter is mys and not the thing mys points to. mys can be modified
    > by this function because it is not declared as being const. I get
    > that you get that, but it was not at all clear from the above!
    >
    >> 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.

    >
    > The reason is that the compiler can't know and thus needs a general
    > rule:
    >
    > struct s { char name[100]; };
    >
    > char *open_up(const struct s *sp)
    > {
    > return sp->name; /* example code: this is an error! */
    > }
    >
    > void safe(void)
    > {
    > struct s data;
    > open_up(&data)[0] = 'a';
    > }
    >
    > void unsafe(void)
    > {
    > const struct s data;
    > open_up(&data)[0] = 'a';
    > }
    >
    > You are right that the function 'open_up' does not allow 'safe' to do
    > anything that it can't already do, but 'usafe' should be stopped. How
    > can the compiler tell the difference? All three can be compiled
    > separately, of course.
    >
    > When an object is declared const, it does not just mean that you won't
    > change it. It also means that you won't hand out the means by which
    > it may be changed (not at least with out a diagnostic message).
    >


    Yep. Got it. Thanks again!


    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #12
  13. On Aug 28, 3:42 pm, Pietro Cerutti <> wrote:
    > i Group,
    >
    > to my understanding, defining a function parameter as "const" means that
    > the function is not going to change it.
    >


    'Const' is a very poor term used in C language that misleads many
    people initially.
    Const means 'Read-Only'. It is a qualifier which states that it cannot
    be changed using
    that particular variable. But, it can be manipulated by accessing the
    address of it using pointers.

    Karthik Balaguru
     
    karthikbalaguru, Aug 28, 2007
    #13
  14. Pietro Cerutti

    Ben Pfaff Guest

    karthikbalaguru <> writes:

    > 'Const' is a very poor term used in C language that misleads many
    > people initially.
    > Const means 'Read-Only'. It is a qualifier which states that it cannot
    > be changed using
    > that particular variable. But, it can be manipulated by accessing the
    > address of it using pointers.


    Actually, the term is 'const'. In C, the case of letters is
    important, so you should take care to be precise.

    There is a little more confusion surrounding the C use of 'const'
    than your article lets on. In particular, its meaning is quite
    different when it is applied to an object definition versus the
    target of a pointer. An object whose definition is qualified
    with 'const' may not be modified; attempting to modify it yields
    undefined behavior. On the other hand, you can always cast
    away 'const' from a 'const'-qualified pointer type, and then
    attempt to modify the object pointed to. Whether that is valid
    depends entirely on how the target object was defined: if it is
    modifiable (not 'const'-qualified, not a string literal, etc.),
    then it is OK.
    --
    Just another C hacker.
     
    Ben Pfaff, Aug 28, 2007
    #14
  15. On Tue, 28 Aug 2007 12:42:42 +0200, Pietro Cerutti <>
    wrote:

    >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(struct my_s *, const char *);
    >char *get_mys_name(const struct my_s *);
    >
    >int main(void) {
    > struct my_s *mysp;
    >
    > mysp = malloc(sizeof *mysp);
    >
    > set_mys_name(mysp, MY_NAME);
    >
    > printf("My name is %s\n", get_mys_name(mysp));
    >
    > return (0);
    >}
    >
    >void
    >set_mys_name(struct my_s *mys, const char *name)
    >{
    > if(!mys) return;
    > strncpy(mys->mys_name, name, MAX_NAME_SIZE);
    >}
    >
    >char *
    >get_mys_name(const 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
    >


    Based on the subsequent discussion, if you want get_mys_name to accept
    a pointer to a non-const struct but still want the function to treat
    it as const (so you get diagnostics if you try to modify it), one
    approach would be

    get_mys_name(struct my_s *pmys) /*const removed, p added*/
    {
    const struct my_s *mys = pmys;
    if(!mys) return (NULL);
    return (pmys->mys_name); /*only use pmys in return*/
    }


    Remove del for email
     
    Barry Schwarz, Aug 30, 2007
    #15
  16. Barry Schwarz wrote:
    > On Tue, 28 Aug 2007 12:42:42 +0200, Pietro Cerutti <>
    > wrote:
    >
    >> 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(struct my_s *, const char *);
    >> char *get_mys_name(const struct my_s *);
    >>
    >> int main(void) {
    >> struct my_s *mysp;
    >>
    >> mysp = malloc(sizeof *mysp);
    >>
    >> set_mys_name(mysp, MY_NAME);
    >>
    >> printf("My name is %s\n", get_mys_name(mysp));
    >>
    >> return (0);
    >> }
    >>
    >> void
    >> set_mys_name(struct my_s *mys, const char *name)
    >> {
    >> if(!mys) return;
    >> strncpy(mys->mys_name, name, MAX_NAME_SIZE);
    >> }
    >>
    >> char *
    >> get_mys_name(const 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
    >>

    >
    > Based on the subsequent discussion, if you want get_mys_name to accept
    > a pointer to a non-const struct but still want the function to treat
    > it as const (so you get diagnostics if you try to modify it), one
    > approach would be
    >
    > get_mys_name(struct my_s *pmys) /*const removed, p added*/
    > {
    > const struct my_s *mys = pmys;
    > if(!mys) return (NULL);
    > return (pmys->mys_name); /*only use pmys in return*/
    > }



    Thank you, unfortunately it doesn't help to reach my goal, which would
    just be to inform the user that the function doesn't modify the
    parameter. I though it could be done using simply the function signature.

    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 30, 2007
    #16
  17. Pietro Cerutti

    CBFalconer Guest

    Pietro Cerutti wrote:
    > Barry Schwarz wrote:
    >

    .... snip ...
    >>
    >> get_mys_name(struct my_s *pmys) /*const removed, p added*/ {
    >> const struct my_s *mys = pmys;
    >> if(!mys) return (NULL);
    >> return (pmys->mys_name); /*only use pmys in return*/
    >> }

    >
    > Thank you, unfortunately it doesn't help to reach my goal, which
    > would just be to inform the user that the function doesn't modify
    > the parameter. I though it could be done using simply the function
    > signature.


    Why the fuss? Simply define the function as:

    char *get_mys_name(struct my_s *mys) /* const removed */ {
    if (mys) return mys->mys_name;
    return NULL;
    }

    No fuss, no muss, no complaints. Ball, keep eye on.

    If the function handles objects that are initially const, use:

    const char *get_mys_name(const struct my_s *mys) {
    if (mys) return mys->mys_name;
    return NULL;
    }

    and now errors will show up at the correct points.

    --
    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
     
    CBFalconer, Aug 30, 2007
    #17
  18. On Thu, 30 Aug 2007 15:17:10 +0200, Pietro Cerutti <>
    wrote:

    >Barry Schwarz wrote:
    >> On Tue, 28 Aug 2007 12:42:42 +0200, Pietro Cerutti <>
    >> wrote:
    >>
    >>> 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(struct my_s *, const char *);
    >>> char *get_mys_name(const struct my_s *);
    >>>
    >>> int main(void) {
    >>> struct my_s *mysp;
    >>>
    >>> mysp = malloc(sizeof *mysp);
    >>>
    >>> set_mys_name(mysp, MY_NAME);
    >>>
    >>> printf("My name is %s\n", get_mys_name(mysp));
    >>>
    >>> return (0);
    >>> }
    >>>
    >>> void
    >>> set_mys_name(struct my_s *mys, const char *name)
    >>> {
    >>> if(!mys) return;
    >>> strncpy(mys->mys_name, name, MAX_NAME_SIZE);
    >>> }
    >>>
    >>> char *
    >>> get_mys_name(const 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
    >>>

    >>
    >> Based on the subsequent discussion, if you want get_mys_name to accept
    >> a pointer to a non-const struct but still want the function to treat
    >> it as const (so you get diagnostics if you try to modify it), one
    >> approach would be
    >>
    >> get_mys_name(struct my_s *pmys) /*const removed, p added*/
    >> {
    >> const struct my_s *mys = pmys;
    >> if(!mys) return (NULL);
    >> return (pmys->mys_name); /*only use pmys in return*/
    >> }

    >
    >
    >Thank you, unfortunately it doesn't help to reach my goal, which would
    >just be to inform the user that the function doesn't modify the
    >parameter. I though it could be done using simply the function signature.


    In that case, go back to your original code and add a *single* cast to
    your return statement.


    Remove del for email
     
    Barry Schwarz, Sep 1, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danny van elsen
    Replies:
    6
    Views:
    41,243
    Pete Becker
    Apr 24, 2005
  2. Jason
    Replies:
    6
    Views:
    9,029
    Mike Wahler
    Oct 9, 2003
  3. Charlie Zender
    Replies:
    12
    Views:
    1,089
    Peter Pichler
    Jan 3, 2004
  4. Neo
    Replies:
    1
    Views:
    556
    Victor Bazarov
    Mar 28, 2006
  5. Andre
    Replies:
    4
    Views:
    24,832
    Andrey Tarasevich
    Mar 4, 2008
Loading...

Share This Page