c89splint

C90 Cast to underlying abstract type


A logging structure that depends on logging related functions looks like this:

typedef struct 
{
    TFkt_vlogf vlogf;
    TFkt_outf outf;
    void* logData;
} TLogger;

In this logging function there is an abstract logData that is assigned with different pointers depending on the job that the logger has.

A Filelogger would at one point access a stored filehandle like this.

FILE * fileHandle = (FILE *)(logger->logData);

Although this compiles SPLint is unhappy about this and complains with this message:

Cast to underlying abstract type FILE *: (FILE *)(logger->logData)

What can i do to satisfy SPLint?

Is there a better way in C90 to store and access data while still keeping the structure signature to pass the type around independent of its implementation?


Solution

  • The better Solution is to use a union and have all possible Data inside that union.

    typedef union
    {
        FILE * fileHandle;
        char something;
        long int other;
    } TLog_data;
    
    typedef struct 
    {
        TFkt_vlogf vlogf;
        TFkt_outf outf;
        TLog_data logData;
    } TLogger;
    

    At some point during execution you would use:

    ((TLogger*) logger)->logData.fileHandle