RPM Community Forums

Mailing List Message of <rpm-devel>

Cast for a union C question.

From: Jeff Johnson <n3npq@mac.com>
Date: Wed 17 Oct 2007 - 17:16:57 CEST
Message-Id: <E6ECD390-7BB0-4493-8AF7-B7EAC3D29145@mac.com>
I'm in the last stages of establishing a typedef for
data returned from headerGetEntry() et al methods.

Since the data is typed, a union is being used:
union hRET_s {
    void * ptr;
    const char ** argv;
    const char * str;
    uint_64 * ui64p;
    uint_32 * ui32p;
    uint_16 * ui16p;
    uint_8 * ui8p;
    int_64 * i64p;
    int_32 * i32p;
    int_16 * i16p;
    int_8 * i8p;
};
typedef union hRET_s rpmTagData;

Since the current API uses "const void *", there is a need to
convert the void * to a union pointer at the moment. I'll fight the
    You changed rpmlib's API!
issues after all the work is actually done.

Well I'm baffled by how to achieve the cast. Everything I've tried  
with casts fails to compile.

This hackery with an extra level of indirection manages to change
the pointer type, but is perhaps overkill:

int headerAddEntry(Header h, int_32 tag, rpmTagType type,
                const void * p, rpmTagCount c)
        /*@modifies h @*/
{
    rpmTagData *q = (void *)&p;
    return (h2hv(h)->hdradd) (h, tag, type, *q, c);
}

So how does one achieve casting from void * to a union?

Inquiring minds want to know ...

73 de Jeff


  • application/pkcs7-signature attachment: smime.p7s
Received on Wed Oct 17 17:17:09 2007
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.