foo_l
foo_l

Reputation: 601

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.

Upvotes: 3

Views: 15303

Answers (3)

yulian
yulian

Reputation: 1627

You have to understand how do allocated pointer works:

  1. Suppose you've allocated memory for three structs Ptr = malloc(3*sizeof(EXE)).
  2. Then when you add 1 to Ptr, it comes to the next struct. You have a block of memory divided by 3 (3 smaller blocks of memory for each struct).
  3. So, need to access to the elements of the 1st struct and then move the pointer to the next one.

Here you can understand how it works:

#include <stdio.h>
#include <stdlib.h>

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

int main()
{
    int i;
    EXE *Ptr;

    Ptr = malloc(3*sizeof(EXE)); // dymnamically allocating the
                                 // memory for three structures
    Ptr->s = "ABC";
    Ptr->d = 'a';

//2nd
    Ptr++;     // moving to the 2nd structure
    Ptr->s = "DEF";
    Ptr->d = 'd';

//3rd
    Ptr++;    // moving to the 3rd structure
    Ptr->s = "XYZ";
    Ptr->d = 'x';

//reset the pointer `Ptr`
    Ptr -= 2; // going to the 1st structure

//printing the 1st, the 2nd and the 3rd structs
    for (i = 0; i < 3; i++) {
        printf("%s\n", Ptr->s);
        printf("%c\n\n", Ptr->d);
        Ptr++;
    }   

    return 0;
}

Notice: - If you have a variable of a struct use . opereator to access to the elements. - If you have a pointer to a struct use -> operator to access to the elements.

     #include <stdio.h>
     #include <stdlib.h>

     struct EXE {
         int a;
     };

     int main(){

    struct EXE variable;
    struct EXE *pointer;

    pointer = malloc(sizeof(struct EXE)); // allocating mamory dynamically 
                                      // and making pointer to point to this
                                      // dynamically allocated block of memory
    // like here
    variable.a = 100;
    pointer->a = 100;

    printf("%d\n%d\n", variable.a, pointer->a);

    return 0;
    }

Upvotes: 2

someone
someone

Reputation: 1696

First you need to allocate some memory for that char * and after that use strcpy library function to copy data for structure element.

p->s = strcpy(s,str);  //where str source, from where you need to copy the data

I hope this will help. Though I can give you full code for that, But I want you to try.

You can use this Dynamically allocate C struct?

and it is a duplicate question.

Upvotes: 1

EnterKEY
EnterKEY

Reputation: 1222

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

Upvotes: 7

Related Questions