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



Jonas.Holmsten@xxxxxxxxx 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>
.



Relevant Pages

  • [patch 6/6] fs: d_hash change for rcu-walk
    ... typedef int; ... static int affs_compare_dentry(const struct dentry *parent, ... const struct dentry *dentry, const struct inode *inode, ...
    (Linux-Kernel)
  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: Separate compilation and access to small integer data in other programs
    ... reasons than `const', of course. ... if the header says `extern const int razzle;' but the definer says ... but the compiler most definitely will.) ... return privateRazzle; ...
    (comp.lang.c)
  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)