473,765 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Jun 1 '07 #1
14 2957
Jo************@ gmail.com 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.
Jun 1 '07 #2
Jo************@ gmail.com 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>
Jun 1 '07 #3
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.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #4
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>
Jun 1 '07 #5
CBFalconer <cb********@yah oo.comwrites:
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.
Jun 2 '07 #6
Jonas.Holms...@ gmail.com 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

Jun 2 '07 #7
Tor Rustad wrote:
>
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?
You can't write to const objects.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 2 '07 #8
Tor Rustad <to********@hot mail.comwrites:
Jo************@ gmail.com 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.
Jun 2 '07 #9
Ben Bacarisse wrote:
CBFalconer <cb********@yah oo.comwrites:
>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.
By George, you're right. I missed that in the declaration.
However ppi is uninitialized, which would be a reason to complain.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 2 '07 #10

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

Similar topics

2
2494
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
5
1544
by: Jason Heyes | last post by:
I didn't want to repeat the same code so I casted to const. Here is a simplified example of when casting to const can help avoid code duplication: class MyClass { int numbers; public: // default constructor not shown
13
1933
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
0
1718
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. I'm able to use this DLL without any problems within my test windows app, but not within my windows service (that's when I receive the casting exception). Here's the code (sorry it's long): VB Function within windows app and windows service:
3
3653
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
5
5929
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
12
2134
by: Phil Endecott | last post by:
Dear Experts, I need a function that takes a float, swaps its endianness (htonl) in place, and returns a char* pointer to its first byte. This is one of a family of functions that prepare different data types for passing to another process. I have got confused by the rules about what won't work, what will work, and what might work, when casting. Specifically, I have an implementation that works until I remove my debugging, at which...
17
2228
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
5
3912
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.