473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Jul 19 '06 #1
8 1424
lw*********@hotmail.com schrieb:
<snip>
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.
Jul 19 '06 #2
lw*********@hotmail.com wrote:
I'm having some fun with const and multiple indirection. Consider the
following:
<snip>
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
Jul 19 '06 #3

lw*********@hotmail.com wrote:
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.

Jul 19 '06 #4

lw*********@hotmail.com wrote:
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?

Jul 20 '06 #5

lw*********@hotmail.com wrote:
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?

Jul 20 '06 #6
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.s...eb07061?hl=en&
--

Frederick Gotham
Jul 20 '06 #7
Bill Pursell <bi**********@gmail.comwrote:
>
lw*********@hotmail.com wrote:
>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
No indeed, but it is not const. It is not even a pointer to a const.

Unless I am missing something, I don't see any problem. The second
parameter is not const, but it must be a pointer to a pointer to const
chars (which is what you have written). Personally, I would write

const char **loc

rather than your way, but they mean exactly the same. The ordering of
"char const" might have misled you?

If the second parameter did not have the const keyword, your function
would break the "constness" of the chars pointed to by the first parameter.

--
Ben.
Jul 22 '06 #8

Ben Bacarisse wrote:
Bill Pursell <bi**********@gmail.comwrote:
/*
* 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

No indeed, but it is not const. It is not even a pointer to a const.

Unless I am missing something, I don't see any problem.
The "problem" is pretty minor. Namely, if you don't
declare it with const, then you get a compiler warning
about the assignment *loc = text. If you do declare it with const,
then the caller must only pass it a pointer to const,
or that call will generate a compiler warning.

Jul 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David | last post by:
On every web browser except Safari, this website works great. (Well, by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac and Windows). The site is: http://www.ruleofthirds.com ...
6
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
5
by: Ryan Ternier | last post by:
I'm having an issue with an SQL insert statement. It's a very simple statement, and it's causing too much fuss. strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden) VALUES("...
3
by: laurenq uantrell | last post by:
I'm trying to return an integer from the following table that returns the number of unique cities: tblEmployees Name City John Boston Frank New York Jim Omaha Betty ...
12
by: NuB | last post by:
The validation controls are giving me a headache. here is what i'm trying to do and so far what I've tried has not worked. I need to hide my grid if the page is not valid how can i accomplish...
5
by: James Ma | last post by:
Now I am debugging a buggy VB.NET function like following Function foo() Try ‘ many loops and codes and nested try catch here! …… Catch (ex as Exception) Msgbox ex.message Finally …
3
by: Echo | last post by:
Hi all. Sorry about the crude headline but this problem is really giving me a headache. See I am currently deloping an app in Visual Studios 2005 C# Lanuage and the thing is like this: I have a...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
8
by: neelsmail | last post by:
Hi, I have just started learning C# ; I was working in C++ till now (and would like to if allowed, but learning new things dont hurt). I am used to specifiying "const" to get the assurance that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.