This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Type problems with C
- To: Jan Hubicka <jh at suse dot cz>
- Subject: Type problems with C
- From: Frank Klemm <pfk at fuchs dot offl dot uni-jena dot de>
- Date: Sun, 9 Sep 2001 00:49:35 +0200
- >Received: (from pfk@localhost)by fuchs.offl.uni-jena.de (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id AAA02791;Sun, 9 Sep 2001 00:49:35 +0200
- Cc: gcc at gcc dot gnu dot org
- References: <20010901202854.A7713@fuchs.offl.uni-jena.de> <20010902000000.C27182@atrey.karlin.mff.cuni.cz> <20010902024104.F7713@fuchs.offl.uni-jena.de> <20010903171717.E13574@atrey.karlin.mff.cuni.cz> <20010904003947.D335@fuchs.offl.uni-jena.de> <20010905133553.E15564@atrey.karlin.mff.cuni.cz> <20010905182009.A339@fuchs.offl.uni-jena.de> <20010907120836.A14477@atrey.karlin.mff.cuni.cz> <20010907221210.G5281@fuchs.offl.uni-jena.de> <20010908182611.N8451@atrey.karlin.mff.cuni.cz>
For data types there is a generic type for generic pointers:
void*
For functions there is no such type because functions are normally
incompatible. But Functions with the same return type and the same parameter
layout can be compatible.
Is it possible to define a type which is compatible with
fn_t, fn_schar_t and fn_uchar_t ?
For data this is void*:
void* p1 =(void*)...;
void* p1 =(char*)...;
void* p1 =(signed char*)...;
void* p1 =(unsigned char*)...;
Is there something similar for function pointers?
------------------------------------------------------------------
int p1_strcmp ( void** p1, void** p2 )
{
signed char* q1 = *(signed char**)p1; <---- ugly typecast
signed char* q2 = *(signed char**)p2; <---- ugly typecast
for ( ; *q1; q1++, q2++ )
if (*q1 != *q2)
return *q1 - *q2;
return *q1 - *q2;
}
int p2_strcmp ( void** p1, void** p2 )
{
unsigned char* q1 = *(unsigned char**)p1; <---- ugly typecast
unsigned char* q2 = *(unsigned char**)p2; <---- ugly typecast
for ( ; *q1; q1++, q2++ )
if (*q1 != *q2)
return *q1 - *q2;
return *q1 - *q2;
}
int p3_strcmp ( signed char** p1, signed char** p2 )
{
signed char* q1 = *p1;
signed char* q2 = *p2;
for ( ; *q1; q1++, q2++ )
if (*q1 != *q2)
return *q1 - *q2;
return *q1 - *q2;
}
int p4_strcmp ( unsigned char** p1, unsigned char** p2 )
{
unsigned char* q1 = *p1;
unsigned char* q2 = *p2;
for ( ; *q1; q1++, q2++ )
if (*q1 != *q2)
return *q1 - *q2;
return *q1 - *q2;
}
typedef int (*fn_t) (void**, void**);
typedef int (*fn_schar_t) (signed char**, signed char**);
typedef int (*fn_uchar_t) (unsigned char**, unsigned char**);
fn_t p1 = p1_strcmp;
fn_t p2 = p2_strcmp;
fn_t p3 = (fn_t)p3_strcmp; <---- ugly typecast
fn_t p4 = (fn_t)p4_strcmp; <---- ugly typecast
fn_t p5 = p3_strcmp; <---- warning or error (C++)
fn_t p6 = p4_strcmp; <---- warning or error (C++)
--
Frank Klemm