Re: const variables
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 30 Dec 2006 08:42:37 -0500
junky_fellow@xxxxxxxxxxx wrote:
[...]
The other thing that I wanted to know whether,
const int *cptr;
int *ptr;
have different types?
They have different types: "const-qualified pointer to int"
and "pointer to int," respectively.
Can we convert one type to other without a cast?
`cptr = ptr' is fine ("adding" a qualifier is all right),
but `ptr = cptr' is not ("subtracting" a qualifier needs a cast).
Also, what's wrong in doing,
void *ptr;
const int *ptr1;
ptr = ptr1;
I believe, "void *" is a generic pointer and any pointer type may be
converted to void pointer without cast. Then, why compiler generates
warning while doing "ptr = ptr1" ?
Because you're trying to "subtract" the const qualifier.
const void *ptr2;
ptr2 = ptr1;
would be fine.
Also, what is the difference between "unqualified type" and "qualified
version of its type" ?
A "qualified" type has one or more "qualifiers" -- const,
restrict, and/or volatile -- while an unqualified type has none.
A qualified type has the same representation, same universe of
values, same alignment requirements, blah, blah, blah, as the
corresponding unqualified type, but the qualifiers specify
"special handling" of various kinds.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- References:
- const variables
- From: junky_fellow@xxxxxxxxxxx
- Re: const variables
- From: Eric Sosman
- Re: const variables
- From: junky_fellow@xxxxxxxxxxx
- const variables
- Prev by Date: Re: typedef forward declare
- Next by Date: Re: without loop printing 1 to n
- Previous by thread: Re: const variables
- Next by thread: MNC Hyderabad is Looking for C++ Developer...
- Index(es):
Relevant Pages
|