Initializing an Array of Variable Sizes of Boolean Elements in C

so I currently have a structure that looks like this:

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;

I also have a create function that looks like this:

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;
}

And from this, I could do something like:

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

Is it possible in C to create an array with a variable size logical elements?

For reference: I need to somehow match the integers either with char*or with bool, so I can call arr[num]and get either a string / word, or the value true / false. In both cases, I'm not sure how to declare, and then initialized to a variable size. Thanks in advance!

+4
source share
5 answers

C99 , , 2 struct.

( 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;
}

: , .

+3

, :

size_t boolsize = 10 * sizeof(bool);

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

malloc ,

+2

, .

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 !

+1

, " ", . * malloc, , :

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

, 0 (false bool), calloc:

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

malloc memset:

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

*), , . , :

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

:

:

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;
}

In addition, you can also save the variable in a structure to store the size of the array arr(along with *arr) so that you can use this later when accessing the array arr.

You will need to manually use free()it when the structure is finished using example.

0
source

Source: https://habr.com/ru/post/1677961/


All Articles