Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize the struct pointer

typedef struct
{
  char *s;
  char d;
}EXE;
EXE  *p;

For the above struct how do I initialize the structure with pointer? I know for a non-pointer we do EXE a[] = { {"abc",1}, {"def",2} }; . Similarly Is it possible with a pointer after allocating the memory? Say like p[] = { {"abc",1},.. so on} . Basically I want to initialize dynamically. Thanks.

like image 981
foo_l Avatar asked Apr 12 '26 12:04

foo_l


1 Answers

We can initialise the structure with pointer as below

example:
 int i;
 char e[5]="abcd";
 EXE *p=malloc(sizeof(*p));
 for(i = 0;i < 5;i++)
   *(p+i)=(EXE){e,i+48};
like image 197
EnterKEY Avatar answered Apr 15 '26 02:04

EnterKEY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!