function returning pointer, could not change data it points to

D

david

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
 
U

Ulrich Eckhardt

david said:
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
 
B

Ben Bacarisse

david said:
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.
 
D

david

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

david

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

Barry Schwarz

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
 
D

david

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
 
A

Amandil

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

Amandil

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top