niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
const headache const
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Thu Jul 20, 2006 1:44 pm    Post subject: Re: const headache const Reply with quote

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

PostPosted: Thu Jul 20, 2006 2:40 am    Post subject: Re: const headache const Reply with quote

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

PostPosted: Thu Jul 20, 2006 2:39 am    Post subject: Re: const headache const Reply with quote

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

PostPosted: Wed Jul 19, 2006 9:59 pm    Post subject: Re: const headache const Reply with quote

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

PostPosted: Wed Jul 19, 2006 7:42 pm    Post subject: Re: const headache const Reply with quote

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

PostPosted: Wed Jul 19, 2006 7:18 pm    Post subject: Re: const headache const Reply with quote

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 Smile

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

PostPosted: Wed Jul 19, 2006 6:47 pm    Post subject: const headache const Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 2:17 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts const char as class member markww C++ 3 Thu Jul 20, 2006 7:55 pm
No new posts Const expression sarathy C 9 Wed Jul 19, 2006 8:35 am
No new posts assignment operator for classes with const data scroopy C++ 19 Mon Jul 17, 2006 8:48 am
No new posts CPANPLUS on debian headache hobosalesman@gmail.com Perl 7 Sun Jul 16, 2006 6:20 am
No new posts Whats the deal with 'const'? Snis Pilbor C 19 Sat Jul 15, 2006 4:34 pm

Credit Check | Mortgages | Debt Help | Consumer information | Northern Rock
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 5.7164s ][ Queries: 20 (5.6233s) ][ GZIP on - Debug on ]