Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function returning pointer, could not change data it points to

Reply
Thread Tools

function returning pointer, could not change data it points to

 
 
david
Guest
Posts: n/a
 
      02-24-2008
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
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      02-24-2008
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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-24-2008
david <(E-Mail Removed)> 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.
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-24-2008
> 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.
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-24-2008
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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-24-2008
On Sun, 24 Feb 2008 10:07:59 -0800 (PST), david
<(E-Mail Removed)> 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
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 12:19 am, david <(E-Mail Removed)> 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.
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 12:19 am, david <(E-Mail Removed)> 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)
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-25-2008
Thanks Amandil, this was exactly what I wanted to know. Thanks to all
of you.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not connect to mail server System.Web Could not access 'CDO.Message' object. ????? Adam Knight ASP .Net 1 12-05-2005 05:38 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Error Message: Outlook Express Could Not Be Started Because Msoe.dll Could Not Be Loaded Dutch Treat Computer Support 4 06-21-2004 02:01 PM
change where a file pointer points, inside a function Don Pasquale C Programming 3 05-10-2004 05:50 PM
Project could not be opened because a language-specific compiler could not be instantieted Roxanne ASP .Net 0 07-04-2003 10:22 AM



Advertisments