This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Type problems with C


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]