2017-04-05 81 views
1

我有一個巨大的結構數組,我創建了不同的功能,根據成員的值來獲得結構指針:我們會根據成員的struct值

typedef struct { 
    uint32_t a; 
    uint32_t b; 
    uint32_t c; 
    uint32_t d; 
} test_t; 

test_t test[10]; // Initialized somewhere else 

static test_t* __get_struct_by_a(uint_32_t a) { 
    for (uint32_t i = 0; i < 10; i++) { 
     if (test[i].a == a) 
      return &test[i]; 
    } 
    return NULL; 
} 

static test_t* __get_struct_by_b(uint_32_t b) { 
    ... 
} 

有沒有一種簡單的方法用C來解決這個代替爲每個成員創建一個查找函數?

回答

0

以下是一種編寫通用函數以獲取(查找)您的第一個匹配結構成員的方法。

我試圖保持與你的示例代碼一致,增加了一些#include指令可能是必要的,一個典型的固定陣列尺寸爲#define這樣TEST_SIZE你不爲循環指數使用硬編碼值。

#include <stddef.h> 
#include <stdio.h> 
#include <string.h> 

typedef struct { 
    uint32_t a; 
    uint32_t b; 
    uint32_t c; 
    uint32_t d; 
} test_t; 

test_t test[10]; // Initialized somewhere else 
#define TEST_SIZE (sizeof(test)/sizeof(test[0])) 

static test_t* __get_struct_by_offset(const void* value_ptr, const size_t offset, const size_t field_size) 
{ 
    for (uint32_t i = 0; i < TEST_SIZE; i++) 
    { 
     if (0 == memcmp(value_ptr, ((unsigned char*)&test[i])+offset, field_size)) 
     { 
      return &test[i]; 
     } 
    } 
    return NULL; 
} 

你會使用這樣的:

uint32_t a_value_to_find = 5; /* example field "a" value to find */ 
    uint32_t b_value_to_find = 10; /* example field "b" value to find */ 
    test_t* test_ptr; 

    /* find specified value for field "a" */ 
    test_ptr = __get_struct_by_offset(&a_value_to_find, offsetof(test_t, a), sizeof(a_value_to_find)); 

    /* find specified value for field "b" */ 
    test_ptr = __get_struct_by_offset(&b_value_to_find, offsetof(test_t, b), sizeof(b_value_to_find)); 

這是你的責任,以確保在指定的offset*value_ptr的數據類型和領域是相同的,因此同樣大小的(field_size )。

爲了簡化使用,您可以編寫一些宏作爲這些調用的簡寫。例如:

#define GET_A(value) __get_struct_by_offset(&value, offsetof(test_t, a), sizeof(value)) 
#define GET_B(value) __get_struct_by_offset(&value, offsetof(test_t, b), sizeof(value)) 

對於查詢 「A」 和 「B」,然後簡化爲:

/* find specified value for field "a" */ 
    test_ptr = GET_A(a_value_to_find); 

    /* find specified value for field "b" */ 
    test_ptr = GET_B(b_value_to_find); 
+0

感謝菲爾這是一個非常巧妙的解決辦法! –