Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Casting int'** to 'const int * const * const' dosn't work, why?

Reply
Thread Tools

Casting int'** to 'const int * const * const' dosn't work, why?

 
 
Jonas.Holmsten@gmail.com
Guest
Posts: n/a
 
      06-01-2007
Hello

I'm porting some C++ stuff to C and having problem to get it through
gcc.
Here is a condensed version of the problem:

void foo(const int * const * const ptr)
{}

main()
{
int i = 5;
int *ptr = &i;

foo(&ptr);
}

The warning is:
warning: passing arg 1 of `foo' from incompatible pointer type.

Can't C implicit cast a int** to 'const int * const * const'? (it
works fine in C++)

/Jonas

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> Hello
>
> I'm porting some C++ stuff to C and having problem to get it through
> gcc.
> Here is a condensed version of the problem:
>
> void foo(const int * const * const ptr)
> {}
>
> main()
> {
> int i = 5;
> int *ptr = &i;
>
> foo(&ptr);
> }
>
> The warning is:
> warning: passing arg 1 of `foo' from incompatible pointer type.
>
> Can't C implicit cast a int** to 'const int * const * const'? (it
> works fine in C++)


Let me pick a nit. C has no "implicit casts". A cast is an operator
and is always explicit in the code.

Short answer your question: no. C does not consider "int **" and
"const int * const * const" to be compatible types. You need a cast:

foo((const int **)&ptr);

The rules for parameter passing derive from the rules for assignment
(in case you want to look up the standard) and they allow qualifiers to
be added at only one level of indirection. So passing a "T *"
argument to a "const T *" pointer is fine, but passing "T **" where
"const T **" is expected is not.

--
Ben.
 
Reply With Quote
 
 
 
 
Tor Rustad
Guest
Posts: n/a
 
      06-01-2007
(E-Mail Removed) wrote:
> Hello
>
> I'm porting some C++ stuff to C and having problem to get it through
> gcc.
> Here is a condensed version of the problem:
>
> void foo(const int * const * const ptr)
> {}
>
> main()
> {
> int i = 5;
> int *ptr = &i;
>
> foo(&ptr);
> }
>
> The warning is:
> warning: passing arg 1 of `foo' from incompatible pointer type.
>
> Can't C implicit cast a int** to 'const int * const * const'? (it
> works fine in C++)


This is really a question for Chris Torek...

Here is a simpler example:

$ cat const_cast.c
int main(void)
{
int **ppi;
const int * const *pcpci;

pcpci = ppi;


return 0;
}
$ cc -ansi -pedantic -Wall -W const_cast.c
const_cast.c: In function âmainâ:
const_cast.c:7: warning: assignment from incompatible pointer type

which requires a diagnostic in C (but perhaps not in C++, since no
const'ness is lost).

In C, two types are compatible if they are the same type (not only if).
A type qualifier (like const), change the type. When it comes to pointer
types, they are compatible, if they point to compatible types.


--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      06-01-2007
CBFalconer wrote:
> Tor Rustad wrote:
> ... snip ...
>> This is really a question for Chris Torek.
>> Here is a simpler example:
>>
>> $ cat const_cast.c
>> int main(void)
>> {
>> int **ppi;
>> const int * const *pcpci;
>>
>> pcpci = ppi;
>> return 0;
>> }

>
> No. const objects must be initialized at declaration time.


Before posting, I even checked that 'pcpci' was a pointer (to const
pointer to const int).

What am I missing exactly?

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-02-2007
CBFalconer <(E-Mail Removed)> writes:

> Tor Rustad wrote:
>>

> ... snip ...
>>
>> This is really a question for Chris Torek.
>> Here is a simpler example:
>>
>> $ cat const_cast.c
>> int main(void)
>> {
>> int **ppi;
>> const int * const *pcpci;
>>
>> pcpci = ppi;
>> return 0;
>> }

>
> No. const objects must be initialized at declaration time.


I may be missing your point, but pcpci is not a const object.

--
Ben.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-02-2007
(E-Mail Removed) wrote:
> Hello
>
> I'm porting some C++ stuff to C
> and having problem to get it through gcc.
> Here is a condensed version of the problem:
>
> void foo(const int * const * const ptr)
> {}


The last const is redundant. Don't cloud the issue.

> main()


Prefer explicit int...

int main(void)

> {
> int i = 5;
> int *ptr = &i;


Replace this with...

const *ptr = &i;

....and it should work.

>
> foo(&ptr);
> }
>
> The warning is:
> warning: passing arg 1 of `foo' from incompatible pointer type.
>
> Can't C implicit cast a int** to 'const int * const * const'?


You mean can't C implicitly _convert_. The conversion is well
defined, however the absense of a cast requires a diagnostic
for a constraint violation.

I recall a thread (I may even have started it!) in csc some
time ago where contributing committee members said that once
a constraint was violated, the code invokes undefined behaviour,
even though the behaviour is otherwise defined, and even if
an implementation succeeds to translate the code.

> (it works fine in C++)


At the end of the day, C is not C++.

--
Peter

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-02-2007
Tor Rustad <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>> Hello
>>
>> I'm porting some C++ stuff to C and having problem to get it through
>> gcc.
>> Here is a condensed version of the problem:
>>
>> void foo(const int * const * const ptr)
>> {}
>>
>> main()
>> {
>> int i = 5;
>> int *ptr = &i;
>>
>> foo(&ptr);
>> }
>>
>> The warning is:
>> warning: passing arg 1 of `foo' from incompatible pointer type.
>>
>> Can't C implicit cast a int** to 'const int * const * const'? (it
>> works fine in C++)

>
> This is really a question for Chris Torek...
>
> Here is a simpler example:
>
> $ cat const_cast.c
> int main(void)
> {
> int **ppi;
> const int * const *pcpci;
>
> pcpci = ppi;
>
>
> return 0;
> }
> $ cc -ansi -pedantic -Wall -W const_cast.c
> const_cast.c: In function âmainâ:
> const_cast.c:7: warning: assignment from incompatible pointer type
>
> which requires a diagnostic in C (but perhaps not in C++, since no
> const'ness is lost).
>
> In C, two types are compatible if they are the same type (not only
> if). A type qualifier (like const), change the type. When it comes to
> pointer types, they are compatible, if they point to compatible
> types.


If it were that simple, then assigning a char * to a const char *
would also be a constraint violation and it is not.

6.5.16.1 Simple assignment
Says that (amongst other things):

-- both operands are pointers to qualified or unqualified versions of
compatible types, and the type pointed to by the left has all the
qualifiers of the type pointed to by the right;

So assignment (and, by extension, parameter passing) is allowed to "add
qualifiers" to the pointed-to type (but not to the type pointed-to by
the pointed-to type).

As far as I can tell, this rule is somewhat arbitrary and is intended,
presumably, to simplify the compiler's job. It (or something like it)
is required because without it a constraint-free program could modify
a const object[1] but at least one other language took the view that
all "safe" assignments would be allowed. Thus the OP's original
example (with const at every level) causes not a peep from a C++
compiler.

Does anyone know the reason C chose this safe but restrictive rule?
Does it significantly simplify the compiler?

[1] Like this:

char *p;
const char foo = 'F';
const char **cp = &p; /* Innocent at first glance */
*cp = &foo;
*p = 'B'; /* Pow! */

This (correctly) raises the alarm from both C and C++ compiler, but
changing the key line to:

const char *const *cp = &p; /* Entirely safe. */

causes C++ to complain only at the now obviously illegal line following
where a now const object is being modified. Both this new line and
the following one are constraint violations as far as C is concerned.

--
Ben.
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      06-02-2007
Ben Bacarisse wrote:

<snip>

> If it were that simple, then assigning a char * to a const char *
> would also be a constraint violation and it is not.


<snip>

That was an excellent clarification, great post Ben!

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      06-02-2007
CBFalconer wrote:
> Tor Rustad wrote:
>> CBFalconer wrote:


[...]

>>> No. const objects must be initialized at declaration time.

>> Before posting, I even checked that 'pcpci' was a pointer (to const
>> pointer to const int).
>>
>> What am I missing exactly?

>
> You can't write to const objects.


I know that, and didn't do such a thing.

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      07-01-2007
On Sat, 02 Jun 2007 05:47:06 +0100, Ben Bacarisse
<(E-Mail Removed)> wrote:
<snip: 6.5.16.1>
> So assignment (and, by extension, parameter passing) is allowed to "add
> qualifiers" to the pointed-to type (but not to the type pointed-to by
> the pointed-to type).
>
> As far as I can tell, this rule is somewhat arbitrary and is intended,
> presumably, to simplify the compiler's job. It (or something like it)
> is required because without it a constraint-free program could modify
> a const object[1] but at least one other language took the view that
> all "safe" assignments would be allowed. Thus the OP's original
> example (with const at every level) causes not a peep from a C++
> compiler.
>
> Does anyone know the reason C chose this safe but restrictive rule?
> Does it significantly simplify the compiler?
>

In ~1989 when const was added they chose they simplest rule that was
obviously safe. It may or may not have significantly simplified the
compiler, but it did significantly simplify programmers', and standard
writers' and voters', rapid understanding of this new feature.

With several years of experience, C++ could choose the somewhat more
flexible rule of adding qualification at all or multiple inner levels.

C99 probably could have followed C++(9 but didn't. I don't know if
it was proposed, although they did have plently of other work to do.
- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments