Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Same pointer parameter const and non-const

 
Thread Tools Search this Thread
Old 11-03-2009, 12:56 AM   #1
Default Same pointer parameter const and non-const


Is this program guaranteed to print 10, or can the compiler
get 'confused' by the const and assume the variable
still has its initial value?

#include <stdio.h>

int func(int *A, int const *B)
{
*A = 10;
return *B;
}

int main()
{
int X = 5;
printf("%d\n", func(&X, &X));
}


Old Wolf
  Reply With Quote
Old 11-03-2009, 01:01 AM   #2
Seebs
 
Posts: n/a
Default Re: Same pointer parameter const and non-const
On 2009-11-03, Old Wolf <> wrote:
> Is this program guaranteed to print 10, or can the compiler
> get 'confused' by the const and assume the variable
> still has its initial value?


I would say it has to print 10. There's no restrict anywhere in sight,
there's questionable aliasing, so I think the compiler's obliged to
assume that modifications of other objects with compatible types could
potentially modify the thing pointed to by B.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-04-2009, 08:27 AM   #3
DanDanDan
 
Posts: n/a
Default Re: Same pointer parameter const and non-const

"Old Wolf" <> wrote in message
news:b5d2ab96-b143-4bb8-8da4-...
> Is this program guaranteed to print 10, or can the compiler
> get 'confused' by the const and assume the variable
> still has its initial value?
>
> #include <stdio.h>
>
> int func(int *A, int const *B)
> {
> *A = 10;
> return *B;
> }
>
> int main()
> {
> int X = 5;
> printf("%d\n", func(&X, &X));
> }


The const will only mean that func won't set B, it won't assume anything
else. The compiler won't get confused either, it will optimise away that
**** completely.




DanDanDan
  Reply With Quote
Old 11-04-2009, 11:54 AM   #4
Ben Bacarisse
 
Posts: n/a
Default Re: Same pointer parameter const and non-const
"DanDanDan" <> writes:

> "Old Wolf" <> wrote in message
> news:b5d2ab96-b143-4bb8-8da4-...
>> Is this program guaranteed to print 10, or can the compiler
>> get 'confused' by the const and assume the variable
>> still has its initial value?
>>
>> #include <stdio.h>
>>
>> int func(int *A, int const *B)
>> {
>> *A = 10;
>> return *B;
>> }
>>
>> int main()
>> {
>> int X = 5;
>> printf("%d\n", func(&X, &X));
>> }

>
> The const will only mean that func won't set B, it won't assume anything
> else.


This wording is a little confusing. B is not const so the function is
permitted to set B (it doesn't, but it might). In fact, there isn't a
single const object anywhere in the program. The const is simply a
promise that func won't use B to change the int that B points to.

<snip>
--
Ben.


Ben Bacarisse
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46