Re: const variables



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

.



Relevant Pages

  • Re: (void**)&ppval
    ... a sort of "generic data pointer type". ... returned from mallochas type "void *", so that it can point to ... void *tmp; ... T *ptr; ...
    (comp.lang.c)
  • Re: Some issue with pointers
    ... typedef struct { ... void *ptr; ... and a pointer to the items structure. ...
    (comp.lang.c)
  • Re: Problem on using a casted void pointer
    ... pointer to which you assign a cast-ed value, ... information the cast might have provided. ... It is still a pointer to void. ... Every other assignment to ptr was from a ...
    (comp.lang.c)
  • Re: Problem on using a casted void pointer
    ... What you have is a void ... pointer to which you assign a cast-ed value, ... information the cast might have provided. ... void* ptr; ...
    (comp.lang.c)
  • Re: replace string
    ... void slash_converter(const char*, const int); ... alter what the pointer points to, ...
    (comp.lang.c)