簡體   English   中英

如何在C中合並兩個typedef結構數組

[英]How to merge two arrays of typedef structs in C

我知道有類似的問題,但我真的被我正在使用結構這一事實絆倒了,而且我一般不太擅長 C。

我有兩個結構數組,我想將它們連接到一個結構數組中。 我正在編寫視頻游戲命令,所以一個數組是:

// Setup_Controller.c
command setup_controller[] = {
    { NOTHING,  250 },
    { TRIGGERS,   5 },
    { NOTHING,  100 },
    { TRIGGERS,   5 },
    { NOTHING,  100 },
};

另一個是

// Get_Eggs.c
command get_eggs[] = {
    // Face and activate the lady
    { LEFT,       5 },
    { NOTHING,    5 },
    { A,          5 },
    .
    . 
    .
    { LEFT,     250 },
    { NOTHING,   30 },
    { UP,        30 },
};

typedef 就在這里:

// Joystick.h

// Declare Object...?
typedef struct {
    Buttons_t button;
    uint16_t duration;
} command; 

我的最終執行發生在這里 // Joystick.c #include <stdlib.h>

#include "Joystick.h"

#include "./Tasks/Setup_Controller.c"
#include "./Tasks/Get_Eggs.c"

command * merge_arrays( command a[],    command b[],
                        size_t a_size,  size_t b_size ) {
    size_t c_size = a_size + b_size; 
    /*allocate new array of sufficient size*/
    command *c = malloc(c_size);
    unsigned i;
    /*copy elements from a into c*/
    for(i = 0; i<a_size; ++i) {
        c[i] = a[i];
    } 
    /*copy elements from b into c*/
    for(i = 0; i < b_size; ++i) {
        c[a_size+i] = b[i];
    }
    
    return c;
}

size_t setup_controller_size    = sizeof(setup_controller);
size_t get_eggs_size            = sizeof(    get_eggs    );

size_t total_size               = sizeof(setup_controller) + sizeof(    get_eggs    );

command step[total_size] = merge_arrays(setup_controller,      get_eggs,
                                        setup_controller_size, get_eggs_size);

我已經用棍子戳這個代碼好幾個小時了。 我幾乎讓它使用了一些關於如何合並數組的代碼,但是當我嘗試獲取這些元素之一的大小時,它一直在拋出錯誤。 那個錯誤是這樣的:

invalid application of 'sizeof' to incomplete type 'command[]' {aka 'struct <anonymous>[]'}

All 而不是數組 A 和數組 B,有一個數組 C,它首先包含 A 的所有元素,然后是 B 的所有元素,保留它們各自的原始順序。 不知道為什么這會變成這樣的麻煩,但我們來了。


最小可重復示例:

//// main.c ////

#include "main.h"

#include "script.c"

#include <stdlib.h>
#include <string.h>

void merge_arrays( data *a, data *b, data *c,
                   size_t a_size,  size_t b_size ) {
    
    memcpy(c, a, a_size);
    memcpy(((unsigned char *)c) + a_size, b, b_size);
}

size_t script1_size = sizeof(script1);
size_t script2_size = sizeof(script2);

merge_arrays(data *script1, data *script2, data *whole_thing,
             size_t a_size, size_t b_size);

.

//// main.h ////

typedef struct {
    int age;
    int height;
} data; 

.

//// script.c  ////

#include "script.h"

data script1[] = {
    { 123,  250 },
    { 123,   5 },
    { 123,  100 },
    { 123,   5 },
    { 123,  100 },
};

data script2[] = {
    { 123,  250 },
    { 123,   5 },
    { 123,  100 },
    { 123,   5 },
    { 123,  100 },
    { 123,  100 },
};

.

//// script.h //// 

extern data script1[];
extern data script2[];

產生的錯誤:

.../Documents/test/main.c:20:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
merge_arrays(data *script1, data *script2, data *whole_thing,
^
/Users/.../Documents/test/main.c:20:1: error: conflicting types for 'merge_arrays'
/Users/.../Documents/test/main.c:10:6: note: previous definition is here
void merge_arrays( data *a, data *b, data *c,
     ^
1 warning and 1 error generated.

a_sizeb_size是以字節為單位的大小,因為這是sizeof產生的sizeof ,但在 C 中數組索引的通常方式中, c[i]以元素數為單位進行計數。 因此,例如,如果command的大小為 4,那么您索引的元素數量是 4 倍。

更改merge_arrays以獲取元素數量的計數可能更慣用,並使用sizeof(get_eggs) / sizeof(command)作為參數調用它。 然后確保調整malloc調用以將其參數乘以sizeof(command)

但更好的是簡單地使用memcpy ,它以字節為單位工作並且可能更有效:

memcpy(c, a, a_size);
memcpy(((unsigned char *)c) + a_size, b, b_size);

c確保+以字節為單位添加,而不是以sizeof(command)為單位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM