Topic: const int * to void * question
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Thu, 1 Jul 1993 07:48:51 GMT Raw View
In article <1993Jun30.180347.1452@rcmcon.com> rmartin@rcmcon.com (Robert Martin) writes:
>dcb@slsvitt (David Binderman 3841) writes:
>>const int cia[2] = { 1, 2 };
>
>>int main() {
>> void * pv;
>
>> const int (*pcia)[2] = &cia;
>> pv = pcia;
>
>> return 0;
>>}
>
>This should be illegal. ARM 4.6 says that you can't cast pointers to
>const items to void*. (Actually this is made clearer in the
>commentary on page 37).
Robert, you are misunderstanding the type of the variable `pcia'.
That variable is a pointer to an array of ints which are const.
It is *not* a pointer to a const array of ints.
I direct your attention to the ANSI C standard, section 3.5.3, fourth para-
graph under "semantics" where this important distinction is further clarified.
--
-- Ronald F. Guilmette ------------------------------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------
Author: erc@netcom.com (Eric Smith)
Date: Thu, 1 Jul 1993 09:55:54 GMT Raw View
In article <rfgC9H71F.DKx@netcom.com> rfg@netcom.com (Ronald F. Guilmette) writes:
>Robert, you are misunderstanding the type of the variable `pcia'.
>
>That variable is a pointer to an array of ints which are const.
>
>It is *not* a pointer to a const array of ints.
>
>I direct your attention to the ANSI C standard, section 3.5.3, fourth para-
>graph under "semantics" where this important distinction is further clarified.
For those who don't have the ANSI C standard handy, would you please
clarify what you mean. How can an array of const ints be different
from a const array of ints? The constness implies both that the array
is const and that the ints are const, regardless of whether it is
considered a const array of ints or an array of const ints. So the two
should be considered exactly the same, shouldn't they?
Also, what is the easiest way to get a copy of the ANSI C standard? Is it
usually sold in bookstores, or is it only by mail order?
Author: torgny@heron.DAFA.SE (Torgny Tholerus SKHB)
Date: 1 Jul 93 13:09:35 GMT Raw View
In article <ercC9HCx6.2ID@netcom.com> erc@netcom.com (Eric Smith) writes:
>
>For those who don't have the ANSI C standard handy, would you please
>clarify what you mean. How can an array of const ints be different
>from a const array of ints? The constness implies both that the array
>is const and that the ints are const, regardless of whether it is
>considered a const array of ints or an array of const ints. So the two
>should be considered exactly the same, shouldn't they?
>
In a pointer declaration, the position of "const" indicates
whether it's the pointer or the object being pointed to that
can't be changed. The qualifier before the pointer modifier
indicates the object being pointed to is "const":
const char *p = buffer; // pointer to const char
p = name; // ok
*p = 'x'; // error!
The qualifier after the modifier indicates the pointer itself
cannot be changed:
char * const p = buffer; // const pointer to char
p = name; // error!
*p = 'x'; // ok
This is taken from Dewhurst/Stark: Programming in C++, section 1.7.
Author: erc@netcom.com (Eric Smith)
Date: Thu, 1 Jul 1993 16:26:20 GMT Raw View
In article <1602@heron.DAFA.SE> torgny@heron.QZ.SE (Torgny Tholerus SKHB) writes:
>In article <ercC9HCx6.2ID@netcom.com> erc@netcom.com (Eric Smith) writes:
>>
>>For those who don't have the ANSI C standard handy, would you please
>>clarify what you mean. How can an array of const ints be different
>>from a const array of ints? The constness implies both that the array
>>is const and that the ints are const, regardless of whether it is
>>considered a const array of ints or an array of const ints. So the two
>>should be considered exactly the same, shouldn't they?
>>
>
> In a pointer declaration, the position of "const" indicates
> whether it's the pointer or the object being pointed to that
That's a different issue, which no one is arguing against.
Author: dcb@slsvitt (David Binderman 3841)
Date: Sat, 26 Jun 93 13:45:02 GMT Raw View
hello there,
Is this legal code ?
const int cia[2] = { 1, 2 };
int main() {
void * pv;
const int (*pcia)[2] = &cia;
pv = pcia;
return 0;
}
I don't think so, but some of the the most trustworthy compilers in the
business think it is. I think the code is legal if the type of pv is
changed to const void *
Pointers to ARM pages or recent ANSI WG papers appreciated.
Regards
David C Binderman dcb@us-es.sel.de Tel: +49 711 821 3841
The Good Old Days - when men were men, women were women and pointers were ints
Author: rmartin@rcmcon.com (Robert Martin)
Date: Wed, 30 Jun 1993 18:03:47 GMT Raw View
dcb@slsvitt (David Binderman 3841) writes:
>const int cia[2] = { 1, 2 };
>int main() {
> void * pv;
> const int (*pcia)[2] = &cia;
> pv = pcia;
> return 0;
>}
This should be illegal. ARM 4.6 says that you can't cast pointers to
const items to void*. (Actually this is made clearer in the
commentary on page 37).
--
Robert Martin | Design Consulting | Training courses offered:
R.C.M. Consulting | rmartin@rcmcon.com | Object Oriented Analysis
2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
Green Oaks IL 60048 | Fax: (708) 918-1023 | C++