function returning pointer, could not change data it points to

Discussion in 'C Programming' started by david, Feb 24, 2008.

  1. david

    david Guest

    I am getting "warning: assignment discards qualifiers from pointer
    target type" error from GCC and I know the error, but I still don't
    know how to correct that.

    I have a function with declaration: char* top(stack *item);
    As you might see I have a stack with string elements. This function
    should allow you to look at the value of the top element in stack and
    it does that, but you can change it and this is not acceptable.

    const char* top(stack *item); // can not change the pointer
    const char* const top(stack *item); or const char* top(stack *item)
    const; // can not change pointer nor data it points to, but I think I
    am going too much in C++ here and I get a lot of errors. I think C
    does not support that, some time ago it did not even support such
    thing as const.

    Any ideas how it should look correctly? Or C still has a weak
    implantation of constants?

    david
     
    david, Feb 24, 2008
    #1
    1. Advertising

  2. david wrote:
    > I am getting "warning: assignment discards qualifiers from pointer
    > target type" error from GCC and I know the error, but I still don't
    > know how to correct that.


    How about you provide the code you get that from? Typically, this comes from
    trying to discard a 'const' qualifier.

    > I have a function with declaration: char* top(stack *item);
    > As you might see I have a stack with string elements.


    No I don't, because you haven't shown the code.

    > const char* top(stack *item); // can not change the pointer


    Wrong. This means that you can not change what the pointer points to.

    > const char* const top(stack *item);


    This is basically meaningless. This means that you can't change the pointer
    returned from the function. However, typically you only copy that pointer
    somewhere and that pointer is then modifyable

    > const char* top(stack *item) const; // can not change
    > pointer nor data it points to,


    This is not valid C.

    > but I think I am going too much in C++ here and I get a lot of errors.


    Well, there this syntax could mean that the object this is invoked on is
    supposed to not be modified,

    Show the code, otherwise nobody can help you unless they have a crystal
    ball.

    Uli
     
    Ulrich Eckhardt, Feb 24, 2008
    #2
    1. Advertising

  3. david <> writes:

    > I have a function with declaration: char* top(stack *item);
    > As you might see I have a stack with string elements. This function
    > should allow you to look at the value of the top element in stack and
    > it does that, but you can change it and this is not acceptable.
    >
    > const char* top(stack *item); // can not change the pointer


    I'd do this, but your comment is wrong (or at best, rather
    misleading). This declares the return value to be a pointer to
    characters that can't be changed (via this pointer, at least). It is
    true the pointer can't can't change but this is because you can't
    "change" the value returned from any function.

    > const char* const top(stack *item);


    This one is (almost) pointless since the return values from functions
    can't be changed anyway. Have you ever seen: int const f(...)?

    > const char* top(stack *item) const;


    As you say, this is not C.

    --
    Ben.
     
    Ben Bacarisse, Feb 24, 2008
    #3
  4. david

    david Guest

    > This is basically meaningless. This means that you can't change the pointer
    > returned from the function. However, typically you only copy that pointer
    > somewhere and that pointer is then modifyable


    I know that, but no one is going to do that with this code.


    The code it self does not mean a lot (at least I think), because the
    only thing I want to know how to return pointer from function and that
    I could not change data using that particular pointer;

    But here is the function:
    char* top(stack *item) {
    child *tmp;

    if (item->root == NULL)
    return NULL;

    tmp = item->root;
    while (tmp != NULL && tmp->next != NULL)
    tmp = tmp->next;

    return tmp->value;
    }

    and small test code fragment:
    stack *kaka;
    char str[20] = "Labas";
    char *kita;
    kaka = createStack(0);
    printf("Dydis: %d\n", size(kaka));
    push(kaka, str);
    printf("Dydis: %d\n", size(kaka));
    kita = top(kaka);
    printf("Tai: %s\n", kita);

    kita[0] = 'B'; would work only I would get that working if top
    function would be with "const" and it still would work.
     
    david, Feb 24, 2008
    #4
  5. david

    david Guest

    Sorry for making mistakes.
    kita[0] = 'B'; works fine if function is not with const, but it works
    and if it is with const, but I am getting that warning.
     
    david, Feb 24, 2008
    #5
  6. On Sun, 24 Feb 2008 10:07:59 -0800 (PST), david
    <> wrote:

    >Sorry for making mistakes.
    >kita[0] = 'B'; works fine if function is not with const, but it works
    >and if it is with const, but I am getting that warning.


    Give us a fighting chance to help you. At no point in any of the code
    you have provided so far do you use the const qualifier. Show us the
    two functions and the exact error message.


    Remove del for email
     
    Barry Schwarz, Feb 24, 2008
    #6
  7. david

    david Guest

    No, I am not using it right now, but I though that I should protect at
    least try to make as much protection as I can, so I change "top"
    function declaration with const:

    const char* top(stack *item) {
    child *tmp;

    if (item->root == NULL)
    return NULL;

    tmp = item->root;
    while (tmp != NULL && tmp->next != NULL)
    tmp = tmp->next;

    return tmp->value;
    }

    And I did not change anything in the main code ("mall test code
    fragment"), I do not want to declare "kita" pointer as const too,
    because I might be using it later for other purposes too.

    The question was very simple: Is it possible to return a pointer
    through which you wound not able directly change the data it points
    and if it is possible, how to do it. Everyone is going around, but no
    one actually is trying to answer it.

    david
     
    david, Feb 25, 2008
    #7
  8. david

    Amandil Guest

    On Feb 25, 12:19 am, david <> wrote:
    > No, I am not using it right now, but I though that I should protect at
    > least try to make as much protection as I can, so I change "top"
    > function declaration with const:
    >
    > const char* top(stack *item) {
    > child *tmp;
    >
    > if (item->root == NULL)
    > return NULL;
    >
    > tmp = item->root;
    > while (tmp != NULL && tmp->next != NULL)
    > tmp = tmp->next;
    >
    > return tmp->value;
    >
    > }
    >
    > And I did not change anything in the main code ("mall test code
    > fragment"), I do not want to declare "kita" pointer as const too,
    > because I might be using it later for other purposes too.
    >
    > The question was very simple: Is it possible to return a pointer
    > through which you wound not able directly change the data it points
    > and if it is possible, how to do it. Everyone is going around, but no
    > one actually is trying to answer it.
    >
    > david


    The proper way to do that would be to declare
    const char *kita;
    kita = top(kaka);

    and you shouldn't be able to use kita to make any changes to kaka-
    >value. However, declaring kita as char *kita (without the const)

    would flag a warning, as would assigning another variable char *yutz =
    kita. As long as you hold the const in the declaration of the pointer
    (read: const char *kita -> kita points to a const char) you should
    have no trouble. (If you still do, check the exact location of the
    error; you may have forgotten a const. Also possible, but unlikely, is
    a compiler error. But let's not go there.)

    Note that even if you do declare the pointer as const, it is still
    possible to circumvent the const by using a cast (yutz = (char
    *)kita).

    So the answer to your question is yes and no. You can return a pointer
    that the compiler would flag when you use it to change the data;
    however, it is not foolproof.

    I hope you found this helpful.

    -- Marty Amandil

    "You can't make anything foolproof, because fools are so ingenious"
    -- Corollary to Murphy's Law.
     
    Amandil, Feb 25, 2008
    #8
  9. david

    Amandil Guest

    On Feb 25, 12:19 am, david <> wrote:
    <snip>

    > And I did not change anything in the main code ("mall test code
    > fragment"), I do not want to declare "kita" pointer as const too,
    > because I might be using it later for other purposes too.
    >


    I forgot to note: Declare kita pointer as const (that is, pointer to
    const data). Just remember you will never be able to change anything
    through kita (though you can reassign kita to point to other char's).
    Sorry, but you can't have it both ways. For another purpose, just use
    a different variable.

    (I may have misunderstood your "using kita for other purpose". If I
    have, forgive me and ignore this post.)

    -- Marty Amandil (Throwing all disregard to the winds)
     
    Amandil, Feb 25, 2008
    #9
  10. david

    david Guest

    Thanks Amandil, this was exactly what I wanted to know. Thanks to all
    of you.
     
    david, Feb 25, 2008
    #10
    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. James.D
    Replies:
    6
    Views:
    406
    James.D
    Mar 5, 2004
  2. Don Pasquale
    Replies:
    3
    Views:
    3,285
    Don Pasquale
    May 10, 2004
  3. Protoman
    Replies:
    14
    Views:
    568
    Protoman
    Dec 11, 2005
  4. G
    Replies:
    3
    Views:
    440
    Sylvester Hesp
    Jan 8, 2007
  5. ais523
    Replies:
    9
    Views:
    318
    David Thompson
    Oct 15, 2007
Loading...

Share This Page