Getting the Address of an RValue inside Macro Definition

I have been working on a map type implementation in C the last couple of days, and wanted to give the map definition the ability to have multiple types. Using void pointers and unions, I’ve been able to accomplish that effect well so far, but I’ve recently run into a problem while writing a function to grab a member of the map.

The problem is that I want the user to be able to pass rvalues to the “GetMapItemValue” function, to avoid the hassle of creating a temporary variable explicitly each time the function is called. The get function, however, takes in a void pointer as an argument, and you cannot get the address of an rvalue.

I can solve this problem by biting the bullet and writing a bunch of different functions for each type case (using some kind of _Generic macro or the like), but I would rather come up with a more pragmatic solution. My code is included below.

Map.h (header file for map implementation)

typedef struct KeyPair
{
    u8 set;
    AmbiguousType key, value;
} KeyPair;

typedef struct Map
{
    u32 max_size, filled_size;
    AmbiguousTypeSpecifier value_type, key_type;
    KeyPair* map_values;
} Map;

KeyPair CreateKeyPair(AmbiguousTypeSpecifier key_type,
                      AmbiguousTypeSpecifier value_type, void* key,
                      void* value);

#define GetMapItemValue(map, key_value)                                        
    __GetMapItemValue(map, (void*)&key_value)
void* __GetMapItemValue(Map* map, void* key_value);

#define GetMapItemValueCharacter(map, key)  *((char*)GetMapItemValue(map, key))
#define GetMapItemValueUnsigned32(map, key) *((u32*)GetMapItemValue(map, key))
#define GetMapItemValueUnsigned64(map, key) *((u64*)GetMapItemValue(map, key))
#define GetMapItemValueSigned32(map, key)   *((i32*)GetMapItemValue(map, key))
#define GetMapItemValueSigned64(map, key)   *((i64*)GetMapItemValue(map, key))

Map.c (definition file for map implementation)

KeyPair CreateKeyPair(AmbiguousTypeSpecifier key_type,
                      AmbiguousTypeSpecifier value_type, void* key, void* value)
{
    KeyPair created = {SUCCESS};
    AssignAmbiguousType(&created.key, key_type, key);
    AssignAmbiguousType(&created.value, value_type, value);

    return created;
}

void* __GetMapItemValue(Map* map, void* key_value)
{
    for (u32 index = 0; index < map->max_size; index++)
    {
        if (!CompareAmbiguousType(&map->map_values[index].key, map->key_type,
                                  key_value))
            continue;

        return GetAmbiguousType(&map->map_values[index].value, map->value_type);
    }
    return NULL;
}

Declarations.h (header file for utilities)

#define SUCCESS 1
#define FAILURE 0

typedef enum AmbiguousTypeSpecifier
{
    character,
    unsigned32,
    unsigned64,
    signed32,
    signed64
} AmbiguousTypeSpecifier;

typedef union AmbiguousType
{
    char character;
    u32 unsigned32;
    u64 unsigned64;
    i32 signed32;
    i64 signed64;
} AmbiguousType;

void AssignAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                         void* value);

void* GetAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member);

u8 CompareAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                        void* value);

Declarations.c (definitions file for utilities)

void AssignAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                         void* value)
{
    switch (member)
    {
        case character:  affected->character = *((char*)value); return;
        case unsigned32: affected->unsigned32 = *((u32*)value); return;
        case unsigned64: affected->unsigned64 = *((u64*)value); return;
        case signed32:   affected->signed32 = *((i32*)value); return;
        case signed64:   affected->signed64 = *((i64*)value); return;
    }
}

void* GetAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member)
{
    switch (member)
    {
        case character:  return (void*)&affected->character;
        case unsigned32: return (void*)&affected->unsigned32;
        case unsigned64: return (void*)&affected->unsigned64;
        case signed32:   return (void*)&affected->signed32;
        case signed64:   return (void*)&affected->signed64;
    }
    return NULL;
}

u8 CompareAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                        void* value)
{
    switch (member)
    {
        case character:
            if (affected->character == *((char*)value)) return SUCCESS;
            return FAILURE;
        case unsigned32:
            if (affected->unsigned32 == *((u32*)value)) return SUCCESS;
            return FAILURE;
        case unsigned64:
            if (affected->unsigned64 == *((u64*)value)) return SUCCESS;
            return FAILURE;
        case signed32:
            if (affected->signed32 == *((i32*)value)) return SUCCESS;
            return FAILURE;
        case signed64:
            if (affected->signed64 == *((i64*)value)) return SUCCESS;
            return FAILURE;
    }
    return FAILURE;
}

Entry.c (entry file for application)

i32 main(void)
{
    ...

    Map* created_map = CreateMap(character, unsigned32, 10);

    AppendMapItem(created_map, 'c', 23);
    printf("%dn", GetMapItemValueUnsigned32(created_map, 'c'));

    DestroyMap(created_map);

    ...

    return 0;
}

The problem rears its ugly head in Entry.c, when the rvalue ‘c’ (aka 99) is passed, and subsequently the address operator is called upon it (&key_value).

I have tried to do all kinds of very strange (to me, a C novice) pointer operations on the value to turn it into a lvalue. I can achieve what I want using the GNU statement expression extension, but this project is cross-platform, which means it must compile with the Visual Studio compiler (CL).

I previously solved this problem in a former function “AppendMapItem” by creating a sort of sub-scope to define temporary variables before getting the address, but since this function must return something, that’s off the table.

Map.h (see above)

// ignore how ugly this is, please
#define AppendMapItem(map, key, value)                                         
    {                                                                          
        __typeof__(key) key_value = key;                                       
        __typeof__(value) value_value = value;                                 
        __AppendMapItem(map, (void*)&key_value, (void*)&value_value);          
    }
void __AppendMapItem(Map* map, void* key, void* value);

New contributor

Zenais is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật