I had a similar question here about allocating and initializing one pointer to struct in a subfunction. Unfortunately I can't extend the good solution I got there to initialize an array of structs. The first element is OK but the second (and all following) elements are zero/NULL.
Here is a commented example. Maybe someone can help me...
#include <stdio.h>
#include <stdlib.h>
typedef struct {int n;} mystruct;
void alloc_and_init_array(mystruct **s)
{
// create an array containing two elements
*s = calloc(sizeof(mystruct), 2);
(*s[0]).n = 100;
(*s[1]).n = 200;
}
int main(void)
{
mystruct *s; // only a pointer. No memory allocation.
alloc_and_init_array(&s);
printf("1st element: %d\n", s[0].n); // here I get 100, that's OK
printf("2nd element: %d\n", s[1].n); // here I get 0. Why?
return 0;
}