| Author |
Message |
Frederick Gotham *nix forums Guru
Joined: 09 Jun 2006
Posts: 502
|
Posted: Thu Jul 20, 2006 1:44 pm Post subject:
Re: const headache const
|
|
|
| Quote: | Why are (int *) and (const int *) compatible but (int **) and (const
int **) are not? Likewise, why are (int **) and (int *const *)
compatible but (int **) and (const int *const *) are not?
|
You might find this helpful:
http://groups.google.ie/group/comp.std.c++/msg/8836b5949eb07061?hl=en&
--
Frederick Gotham |
|
| Back to top |
|
 |
lovecreatesbeauty *nix forums Guru Wannabe
Joined: 21 Feb 2005
Posts: 193
|
Posted: Thu Jul 20, 2006 2:40 am Post subject:
Re: const headache const
|
|
|
lwawrzyniak@hotmail.com wrote:
| Quote: | Why are (int *) and (const int *) compatible but (int **) and (const
int **) are not?
|
int * is a pointer to an int object;
const int * is a pointer to an int object[1];
So, they are the pointers of same type, the pointer are compatible.
An int * object and a const int * object are different object types[2],
so pointers to them are pointers to different object types, the pointer
are incompatible.
I learnt these from a post of Ian Collins', do I understand it
correctly?
--
Questions on that:
[1] I mean it should be: ... to a CONST int object[1];
[2] then an int object and const int object are also different,
pointers to them are different: int * and const int * are different
and incompatible, right? |
|
| Back to top |
|
 |
lovecreatesbeauty *nix forums Guru Wannabe
Joined: 21 Feb 2005
Posts: 193
|
Posted: Thu Jul 20, 2006 2:39 am Post subject:
Re: const headache const
|
|
|
lwawrzyniak@hotmail.com wrote:
| Quote: | Why are (int *) and (const int *) compatible but (int **) and (const
int **) are not?
|
int * is a pointer to an int object;
const int * is a pointer to an int object[1];
So, they are the pointers of same type, the pointer are compatible.
An int * object and a const int * object are different object types[2],
so pointers to them are pointers to different object types, the pointer
are incompatible.
I learnt these from a post of Ian Collins', do I understand it
correctly?
--
Questions on that:
[1] I mean it should be: ... to a CONST int object[1];
[2] then an int object and const int object are also different,
pointers to them are different: int * and const int * are different
and incompatible, right? |
|
| Back to top |
|
 |
bill *nix forums Guru Wannabe
Joined: 12 May 2005
Posts: 241
|
Posted: Wed Jul 19, 2006 9:59 pm Post subject:
Re: const headache const
|
|
|
lwawrzyniak@hotmail.com wrote:
| Quote: | I'm having some fun with const and multiple indirection. Consider the
following:
snip
How can I "constify" data pointed to by a function parameter at all
levels of indirection? (Other than wrapping it up in structs)
|
I have a related question. Consider the following:
/*
* Set loc (location) to the first occurence of target
* in text. Return 0 on success, -1 on failure.
*/
int
locate(const char *text, char const **loc, char target)
{
int ret = -1;
for ( ; *text; text++) {
if (*text == target) {
*loc = text;
ret = 0;
break;
}
}
return ret;
}
In the declaration of locate, we definitely want
text to be declared with const. The subsequent
assignment of loc "poisons" loc so that it needs
to be const. But someone calling such a function
generally doesn't really want that 2nd argument
to be const...so what's a person to do? Either
the caller has to pass a const in for the 2nd arg,
or cast it, or take the compiler warning. |
|
| Back to top |
|
 |
Michael Mair *nix forums Guru
Joined: 19 Feb 2005
Posts: 963
|
Posted: Wed Jul 19, 2006 7:42 pm Post subject:
Re: const headache const
|
|
|
lwawrzyniak@hotmail.com schrieb:
<snip>
| Quote: | void f(const int * const * a) {
// *a is read-only
// **a is read-only
}
int main(void) {
int **a = NULL;
f(a); // <-- compiler warning:
// "passing arg 1 from incompatible pointer type"
// (undesired behaviour)
return 0;
}
Why are (int *) and (const int *) compatible but (int **) and (const
int **) are not? Likewise, why are (int **) and (int *const *)
compatible but (int **) and (const int *const *) are not?
How can I "constify" data pointed to by a function parameter at all
levels of indirection? (Other than wrapping it up in structs)
snip |
Have a look at
http://c-faq.com/ansi/constmismatch.html
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. |
|
| Back to top |
|
 |
Flash Gordon *nix forums Guru
Joined: 28 Feb 2005
Posts: 1258
|
Posted: Wed Jul 19, 2006 7:18 pm Post subject:
Re: const headache const
|
|
|
lwawrzyniak@hotmail.com wrote:
| Quote: | I'm having some fun with const and multiple indirection. Consider the
following:
|
<snip>
| Quote: | Please refrain from suggesting that I should just give up on using
const altogether. That would be too easy
|
There is a lot about this in the comp.lang.c FAQ, a document you should
always check before asking a question here. I suggest you read question
11.10 and then come back with any further questions.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc |
|
| Back to top |
|
 |
lpw *nix forums beginner
Joined: 01 Nov 2005
Posts: 10
|
Posted: Wed Jul 19, 2006 6:47 pm Post subject:
const headache const
|
|
|
I'm having some fun with const and multiple indirection. Consider the
following:
int *a; /* pointer to an integer */
const int *b; /* pointer to a constant integer */
b = a; /* ok */
int **c; /* pointer to (pointer to an integer) */
const int **d; /* pointer to (pointer to a constant integer) */
d = c; /* not ok (incompatible pointer type) */
int *const *e; /* pointer to (constant pointer to an integer) */
e = c; /* ok */
This is important to me because I am trying to pass a dynamically
allocated two-dimensional "array" to a function that is not allowed to
modify the data at any level of indirection.
void f(const int * const * a) {
// *a is read-only
// **a is read-only
}
int main(void) {
int **a = NULL;
f(a); // <-- compiler warning:
// "passing arg 1 from incompatible pointer type"
// (undesired behaviour)
return 0;
}
Why are (int *) and (const int *) compatible but (int **) and (const
int **) are not? Likewise, why are (int **) and (int *const *)
compatible but (int **) and (const int *const *) are not?
How can I "constify" data pointed to by a function parameter at all
levels of indirection? (Other than wrapping it up in structs)
Please refrain from suggesting that I should just give up on using
const altogether. That would be too easy :)
Thanks,
Lukasz |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|