I'm trying to initialize an struct array using designated initializer:
A[size].from = {[0 ... size - 1] = 0};
A[size].to = {[0 ... size - 1] = 0};
List.h:
typedef struct Buffer
{
int from;
int to;
}buffer_t;
typedef struct shared_memory
{
buffer_t A;
int size;
} share_t
Main.c
#include <stdio.h>
#include "list.h"
buffer_t A[];
int size;
void main(int argc, char *argv[])
{
share->share_t *share = (share_t *)malloc(sizeof(share_t));
long arg = strtol(argv[1], NULL, 10);
size = arg;
share->A[size] = {[0 ... size-1] = 0};
}
However this is the following error I'm receiving:
I'm using MinGW gcc and command prompt to compile this code and I'm writing this code in Visual studio.
Your code is an assignment expression, not an initializer. Initialization can only happen when an object is defined.
Also, arrays must be defined with a size. And there are better ways to zero-initialize an object than to attempt to use designated initializer ranges.
Here are two options:
int main(int argc, char *argv[])
{
long size = strtol(argv[1], NULL, 10);
buffer_t A[size];
memset(&A, 0, sizeof A);
or
buffer_t *A;
size_t size;
int main(int argc, char *argv[])
{
size = strtol(argv[1], NULL, 10);
A = calloc(size, sizeof *A); // already zero-initialized
The latter is probably better in that you can check for allocation failure and it supports large allocations.