c - Array of structs with multiple members -


in struct "saf" have array "stack" , in struct "data" have "pinx" array of struct "data" . want array "stack" have members "pinx" array, don't know how can have access stack array members of pinx. provide image understand want do.

enter image description here

#include <stdio.h> #include <stdlib.h> #include <string.h>  struct saf {   int head;   void **stack;   int size; }exp1;  struct data {   int flag;   char name[50]; };   struct data *pinx;  void init(int n) {    pinx = malloc(sizeof(struct data) * n);   exp1.stack = malloc(n);  }  void add() {   strcpy(pinx[0].name,"name");  pinx[0].flag=0;  exp1.stack[0]=&pinx[0];  }  int main() {   printf("give size: ");  scanf("%d",&exp1.size);  init(exp1.size);  add();   return 0; } 

just use

void *stack; 

in saf struct.

exp1.stack = malloc(n); //is not needed 

then

exp1.stack = (void *)pinx; 

and accessing elements

printf("%s \n", ((struct data *)exp1.stack)[0].name); 

valter


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -