3
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.

foo_l
  • 591
  • 2
  • 10
  • 28
  • Take a good book to avoid asking **such questions** in future. – yulian Aug 05 '13 at 10:55
  • What is that I am missing, can you please point? Is it that basic/novice level question? Thanks. – foo_l Aug 05 '13 at 10:58
  • Look my updated answer. Anyway, I advise you to read about `pointers` because it seems that you haven't understanding of how pointers work. This is difficult (pointers) but it is worth spending several hours to read about them. Hope you'll succeed;) – yulian Aug 05 '13 at 12:04
  • And there is no difference of "the level" the question. – yulian Aug 05 '13 at 12:14

3 Answers3

7

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};
EnterKEY
  • 1,180
  • 3
  • 11
  • 25
  • Thanks for the reply. Why are you incrementing `i` to `48`? – foo_l Aug 05 '13 at 10:56
  • OK. got it. you have tried assigning some value. But this will store only abcd string in the allocated memory. What if I want to have different strings and values. thanks again. – foo_l Aug 05 '13 at 11:06
  • 2nd element of structure is character right,so adding ascii value of 0 =48 ,so we have character 0,1,2,3,4 – EnterKEY Aug 05 '13 at 11:06
  • 1
    you declare array of char pointers for 1st member of structure and array of integer for second member and dynamically give the value. char *e[5]; char s[10]; for(j=0;j<5;j++) { scanf("%s",s); *(e+j)=malloc(sizeof(s)); strcpy(*(e+j),s); } – EnterKEY Aug 05 '13 at 12:04
  • I was wondering, i know that sometimes you should initialise the struct with memset to fill with 0's, should the same be done with a struct pointer? whatever the reason please explain why. personally i don't think it should, but i need assurance. – Evgeny Danilenko Dec 20 '16 at 21:15
1

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.

Community
  • 1
  • 1
someone
  • 1,638
  • 3
  • 21
  • 36
  • Thanks for the reply. I have not asked for dynamic allocation of struct. I want to initialise the allocated struct as I mentioned in the example. – foo_l Aug 05 '13 at 10:54
  • @foo_l... I want that you should write code for that. Up to my understanding I am right... One more example for you http://stackoverflow.com/questions/6296880/initializing-a-pointer-to-a-structure Just search you will get hundreads of question like that. – someone Aug 05 '13 at 11:06
1

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;
    }
yulian
  • 1,601
  • 3
  • 21
  • 49
  • I aware of those basics. Seems you did not understand my question. Or may be I am not clear in explaining. Seems ReDEyE understood and is close enough. Thanks for the post anyways. :) – foo_l Aug 05 '13 at 12:31