2017-05-26 84 views
4

所以我現在有一個結構,看起來像這樣:初始化布爾值的大小可變的數組用C

typedef struct example { 
    bool arr[]; //i would like this to be an array of booleans, 
       //but i don't know the size right now; this is 
       //probably wrong 
} example_t; 

我也有一個創建功能,看起來像這樣:

example_t *newExample(int SIZE){ 
    example_t *example = malloc(sizeof(example_t)); 
    //initialize arr to a bool array of size SIZE, ideally all false! 
    example->arr = ??? 
    return example; 
} 

而且從這樣,我就可以做這樣的事情:

example_t *example = newExample(MAX); 
if (example->arr[10]) 
     .... 

這是可能的C,創建一個可變大小的布爾值數組?

僅供參考:我需要好歹整數映射到一個char*bool,所以我可以調用arr[num]並能夠得到一個字符串/字或真/假值。兩種方式,我不知道如何聲明,然後用可變大小初始化。提前致謝!

回答

3

在C99中,您可以使用flexible array members,其中最後一個成員可以是沒有給定維度的數組,但struct中必須至少有2個成員。

您可以使用指針(這裏我使用int,而不是bool爲了簡潔):

#include <stdlib.h> 
#include <stdio.h> 

typedef struct example { 
    int *arr; 
} example_t; 

static example_t *newExample(size_t SIZE) 
{ 
    example_t *example = malloc(sizeof(example_t) + sizeof(int) * SIZE); 

    example->arr = (int *)(example + 1); 
    example->arr[5] = 5; 
    return example; 
} 

int main(void) 
{ 
    example_t *x = newExample(10); 

    printf("%d\n", x->arr[5]); 
    free(x); 
    return 0; 
} 

但是這並沒有太大的意義,爲什麼不添加包含的元素數量的第一部件?

#include <stdlib.h> 
#include <stdio.h> 

typedef struct example { 
    size_t size; 
    int arr[]; 
} example_t; 

static example_t *newExample(size_t SIZE) 
{ 
    example_t *example = malloc(sizeof(example_t) + sizeof(int) * SIZE); 

    example->size = SIZE; 
    example->arr[5] = 5; 
    return example; 
} 

int main(void) 
{ 
    example_t *x = newExample(10); 

    printf("%d\n", x->arr[5]); 
    free(x); 
    return 0; 
} 

使用這種方法富人的優勢:您可以將對象傳遞給任何功能,無需經過額外的參數大小。

+0

我認爲OP要求'布爾'數組初始化,而不是'int'數組。但是,這個概念是相同的。 – Muntasir

+0

@Muntasir,是的,概念是一樣的,無論如何我編輯答案... –

1

如果您事先不知道數組的大小,您可以簡單地將它作爲指針並在稍後進行分配。

typedef struct example { 
    bool* arr; // <-- Pointer 
    int size; // <-- Size of the array 
} example_t; 

創建一個新的數組遵循相同的步驟。

example_t* newExample(int size) { 
    int i; 
    example_t* example = malloc(sizeof(example_t)); 
    example->arr = malloc(sizeof(bool) * size); 
    example->size = size; 
    for(i = 0; i < size; i++) 
     example->arr[i] = false; 
    return example; 
} 

不要忘記釋放bool arr和使用後struct本身的記憶!

2

可以通過例如後加條內存做到這一點:

size_t boolsize = 10 * sizeof(bool); 

struct example *ptr = malloc(sizeof *ptr + boolsize); 

你調用malloc只有一次,此內存方式奠定了連續

0

做這些東西:

結構定義:

typedef struct example { 
    bool *arr; 
} example_t; 

創建:

example_t *newExample(int SIZE){ 
    example_t *example = malloc(sizeof(example_t)); 
    example->arr = malloc(SIZE*sizeof(bool)) // allocate space for arr 
    for(int i = 0;i < SIZE;i++) 
     example->arr[i] = false; // initialize all of arr elements to false 
    return example; 
} 

可選地,也可以保持變量在struct存儲arr陣列(沿着*arr)的大小,這樣就可以使用該訪問arr陣列時以後。

當結構example的使用完成時,您將不得不手動使用free()

1

允許結構的最後一個成員是未指定大小的數組,因此稱爲「靈活數組成員」,因此您的聲明是正確的。*您有責任計算malloc的正確大小,因此在您的情況下,這樣做:

example_t* example = malloc(sizeof(example_t) + SIZE * sizeof(bool)); 

如果你想它(在一個典型的布爾型false)初始化爲0,要麼使用calloc

example_t* example = calloc(1, sizeof(example_t) + SIZE * sizeof(bool)); 

或者使用memset的malloc後初始化:

memset(example, 0, sizeof(example_t) + SIZE * sizeof(bool)); 

*)只要您的結構中還有其他成員在數組之前。否則結構將是無用的,你可以只分配普通數組:

bool *example = malloc(SIZE * sizeof(bool));