简体   繁体   English

初始化struct指针

[英]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? 对于上面的struct ,我如何用指针初始化结构? I know for a non-pointer we do EXE a[] = { {"abc",1}, {"def",2} }; 我知道一个非指针我们做EXE a[] = { {"abc",1}, {"def",2} }; . Similarly Is it possible with a pointer after allocating the memory? 同样在分配内存后是否可以使用指针? Say like p[] = { {"abc",1},.. so on} . 比如说p[] = { {"abc",1},.. so on} Basically I want to initialize dynamically. 基本上我想动态初始化。 Thanks. 谢谢。

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

First you need to allocate some memory for that char * and after that use strcpy library function to copy data for structure element. 首先,您需要为该char *分配一些内存,然后使用strcpy库函数来复制结构元素的数据。

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? 您可以使用此动态分配C结构吗?

and it is a duplicate question. 这是一个重复的问题。

You have to understand how do allocated pointer works: 您必须了解分配指针的工作原理:

  1. Suppose you've allocated memory for three structs Ptr = malloc(3*sizeof(EXE)) . 假设您为三个结构分配了内存Ptr = malloc(3*sizeof(EXE))
  2. Then when you add 1 to Ptr, it comes to the next struct. 然后当你向Ptr添加1时,它会进入下一个结构。 You have a block of memory divided by 3 (3 smaller blocks of memory for each struct). 你有一块内存除以3(每个结构有3个较小的内存块)。
  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. opereator访问元素。 - 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM