473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help on type compatibility?

Ark
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What's
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)

Thank you,
- Ark
Feb 24 '06 #1
29 2927

Ark wrote:
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What's
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)


Part of one of the clauses of 3.5.4.3 states:
[1] "For two function types to be compatible, both
shall specify compatible return types. Moreover,
the parameter type lists, if both are present, shall
agree in the number of parameters and in use of
the ellipsis terminator; corresponding parameters
shall have compatible types."

So we need to ascertain whether or not their parameters
have compatible type:

'Is char * compatible with const char *?'

3.5.4.1 from one of its clauses, states:

"For two pointer types to be compatible, both
shall be identically qualified and both shall
be pointers to compatible types."

So their compatibility is predicated upon
whether or not they
1) point to compatible types
2) share the same qualifications

2 does not hold here, and therefore the
types are not compatible. The implication
is then that [1] does not hold.

--
aegis

Feb 24 '06 #2
Ark
aegis wrote:
Ark wrote:
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What's
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated .)

Part of one of the clauses of 3.5.4.3 states:
[1] "For two function types to be compatible, both
shall specify compatible return types. Moreover,
the parameter type lists, if both are present, shall
agree in the number of parameters and in use of
the ellipsis terminator; corresponding parameters
shall have compatible types."

So we need to ascertain whether or not their parameters
have compatible type:

'Is char * compatible with const char *?'

3.5.4.1 from one of its clauses, states:

"For two pointer types to be compatible, both
shall be identically qualified and both shall
be pointers to compatible types."

So their compatibility is predicated upon
whether or not they
1) point to compatible types
2) share the same qualifications

2 does not hold here, and therefore the
types are not compatible. The implication
is then that [1] does not hold.

--
aegis

Thank you, aegis.
But...
Is it indeed that char * is not compatible with const char * ?
char * is unqualified pointer to char; const char * is unqualified
pointer to const char. So the question reduces to whether char is
compatible with const char, and I think it is... After all, we routinely
write
char c;
const char cc;
char *p;
const char *cp;
..............
c = cc;
cp = p;
I beg for further clarification.. .
Thank you,
Ark
Feb 24 '06 #3

"Ark" <ak*****@macroe xpressions.com> wrote in message
news:E_******** *************** *******@comcast .com...
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What's
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)


The assignment is legal because it's portable to convert a function pointer
to a different function pointer type and back again. (This can avoid the
need to use unions of function pointer types, which would be pointlessly
clumsy.)

But it's well worth a warning at this point, because no warning can be given
later if you then call the function through the pointer of the wrong type,
and that would produce undefined behaviour.

The problem is that the caller and the callee must agree implicitly on all
the details of how the function call is actually set up. The compiler makes
its own rules about that, and it's allowed to refer to the types of the
arguments, including qualifiers, if there's a prototype. So calling through
the wrong prototype can produce a mismatch in the calling sequence.

In this case it's likely that the undefined behaviour produced is to do the
right thing on all implementations , unless somebody knows different...

--
RSH

Feb 24 '06 #4
Ark wrote:
char * is unqualified pointer to char; const char * is unqualified
pointer to const char.


No.

N869
6.2.5 Types
[#26]
Each
unqualified type has several qualified versions of its
type, corresponding to the combinations of one, two, or
all three of the const, volatile, and restrict qualifiers.

--
pete
Feb 24 '06 #5
pete wrote:
Ark wrote:
char * is unqualified pointer to char; const char * is unqualified
pointer to const char.


No.


Yes.

Feb 24 '06 #6
In article <PI************ ********@comcas t.com>
Ark <ak*****@macroe xpressions.com> wrote:
Is it indeed that char * is not compatible with const char *?
Yes, it is indeed not compatible.
[yet] we routinely write
char *p;
const char *cp;
............ .
cp = p;
I beg for further clarification.. .


There is a special rule for assignment (including the implied
assignment from prototyped function calls) that relaxes the
constraints, so that you can do "cp = p", but not "p = cp".

This special rule does *not* apply to other cases, so:

char **pp;
...
pp = &cp;

requires a diagnostic.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 24 '06 #7
>"Ark" <ak*****@macroe xpressions.com> wrote in message
news:E_******* *************** ********@comcas t.com...
My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

In article <dt**********@n ewsg1.svr.pol.c o.uk>
Robin Haigh <ec*****@leeds. ac.uk> wrote:The assignment is legal because ...
The assignment requires a diagnostic. GCC (some versions anyway)
just gets this wrong.

I think you are thinking of the version with casts:

test_t *mypointer = (test_t *)somefunction;

which does not require a diagnostic.
The problem is that the caller and the callee must agree implicitly on all
the details of how the function call is actually set up.


This, however, is correct.

The Standard has some wording that implies (or perhaps even "requires"
if you believe the footnotes, but the footnotes are "non-normative",
meaning an Evil Implementor can violate them without breaking the
rules) ... er, where was I? Oh yes: qualified and unqualified
variants of types are *supposed* to have the same underlying
representations , so one might simply assume that test_t and contest_t
are effectively interchangeable , and get away with it. It is quite
unlikely to break. Still, it is best not to skate on the thin ice.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 24 '06 #8
mark wrote:

pete wrote:
Ark wrote:
char * is unqualified pointer to char; const char * is unqualified
pointer to const char.


No.


Yes.


Idiot.
N869
6.2.5 Types
[#26]
Each
unqualified type has several qualified versions of its
type, corresponding to the combinations of one, two, or
all three of the const, volatile, and restrict qualifiers.


--
pete
Feb 24 '06 #9
On 2006-02-24, Chris Torek <no****@torek.n et> wrote:
"Ark" <ak*****@macroe xpressions.com> wrote in message
news:E_****** *************** *********@comca st.com...
My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

In article <dt**********@n ewsg1.svr.pol.c o.uk>
Robin Haigh <ec*****@leeds. ac.uk> wrote:
The assignment is legal because ...


The assignment requires a diagnostic. GCC (some versions anyway)
just gets this wrong.

I think you are thinking of the version with casts:

test_t *mypointer = (test_t *)somefunction;

which does not require a diagnostic.
The problem is that the caller and the callee must agree implicitly on all
the details of how the function call is actually set up.


This, however, is correct.

The Standard has some wording that implies (or perhaps even "requires"
if you believe the footnotes, but the footnotes are "non-normative",
meaning an Evil Implementor can violate them without breaking the
rules)


Even if the footnote is prescribing an interpretation of normative text,
rather than providing new rules?
... er, where was I? Oh yes: qualified and unqualified variants of
types are *supposed* to have the same underlying representations , so
one might simply assume that test_t and contest_t are effectively
interchangeable , and get away with it. It is quite unlikely to break.
Still, it is best not to skate on the thin ice.

Feb 24 '06 #10

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

Similar topics

7
8695
by: Mike | last post by:
I've been trying for the past week to put a simple code together. I have done a LOT of searching, found scripts showing the functions I would like to use, however when I mix them it all goes wrong, somehow I always end up with error messages and functions not working right. Can someone please help me? I have a form, inside is 1 Text Field and 2 Password Fields. What I'm looking to do is: - Make sure password fields are equal - Set...
7
1983
by: Trvl Orm | last post by:
I am working with 2 frames, Left and Right and the main code is in the left frame, which has been attached. Can someone please help me with this code. I am new to JavaScript and can't figure it out. What needs to happen is this: On the left frame you should have a series of buttons, which when pushed makes things happen on the right frame.
8
3393
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some problems. IE crashes when one refreshes the page or leave the page. This happens only after calling the Java method more than once. It does not crash if the Java method is called just once and then the page is refreshed. FireFox does not crash at all...
5
3001
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
8
5478
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
16
2452
by: Randy Yates | last post by:
I have an Access database that has been running fine on multiple platforms (Windows 2000, Windows XP, etc.) for several years. Recently, the database has begun to issue "Run-Time Error 2467" on one XP platform when certain forms are opened. Other forms result in a total application crash with the resulting "Bug Report" dialog sequence. The same forms within the same application run fine under Win2000. I'm wondering if this is due to...
1
3717
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
2
1467
by: Serge Rielau | last post by:
Hi folks, My fellow team mates had some extra time on their hands so we decided to spice up DB2 with a grab-bag of compatibility features. We wouldn't mind help validating the semantics match.... So for those of you blessed or cursed with a competitive DBMS here is a list of what we've added: 1. ** DUAL Always works without prefixing a schema
7
2371
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using <html> <script...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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...
1
6800
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
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
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.