简体   繁体   English

如何在 C 中初始化指向结构的指针?

[英]How to initialize a pointer to a struct in C?

Given this struct:给定这个结构:

struct PipeShm
{
    int init;
    int flag;
    sem_t *mutex;
    char * ptr1;
    char * ptr2;
    int status1;
    int status2;
    int semaphoreFlag;

};

That works fine:效果很好:

static struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , 
        .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , 
        .semaphoreFlag = FALSE };

But when I declare static struct PipeShm * myPipe , that doesn't work, I'm assuming that I'd need to initialize with the operator -> , but how?但是当我声明static struct PipeShm * myPipe时,这不起作用,我假设我需要使用运算符->进行初始化,但是如何?

static struct PipeShm * myPipe = {.init = 0 , .flag = FALSE , .mutex = NULL , 
        .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , 
        .semaphoreFlag = FALSE };

Is it possible to declare a pointer to a struct and use initialization with it?是否可以声明一个指向结构的指针并对其进行初始化?

You can do it like so: 您可以这样做:

static struct PipeShm * myPipe = &(struct PipeShm) {
    .init = 0,
    /* ... */
};

This feature is called a "compound literal" and it should work for you since you're already using C99 designated initializers. 此功能称为“复合文字”,由于您已经在使用C99指定的初始化程序,因此它应该对您有用。


Regarding the storage of compound literals: 关于复合文字的存储:

6.5.2.5-5 6.5.2.5-5

If the compound literal occurs outside the body of a function, the object has static storage duration; 如果复合文字出现在函数主体之外,则对象具有静态存储持续时间; otherwise, it has automatic storage duration associated with the enclosing block. 否则,它具有与封闭块关联的自动存储时间。

Is it possible to declare a pointer to a struct and use initialization with it ? 是否可以声明一个指向结构的指针并对其进行初始化?

Yes. 是。

const static struct PipeShm PIPE_DEFAULT = {.init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL ,
        .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE };

static struct PipeShm * const myPipe = malloc(sizeof(struct PipeShm));
*myPipe = PIPE_DEFAULT;

First you need to allocate memory for the pointer as below: 首先,您需要为指针分配内存,如下所示:

myPipe = malloc(sizeof(struct PipeShm));

Then, you should assign values one by one as below: 然后,您应该按如下所示逐一分配值:

myPipe->init = 0;
myPipe->flag = FALSE;
....

Please note that for each individual pointer inside the structure, you need allocate memory seperately. 请注意,对于结构中的每个单独的指针,您需要单独分配内存。

Okay I got it : 好吧,我明白了:

static struct PipeShm  myPipeSt = {.init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL ,
        .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE };

static struct PipeShm  * myPipe = &myPipeSt;

First initialize the struct ( static struct PipeShm myPipe = {... ). 首先初始化结构( static struct PipeShm myPipe = {... )。 Then take the address 然后取地址

struct PipeShm * pMyPipe = &myPipe;

you have to build that struct by hand, and then make a pointer pointing to that. 您必须手动构建该结构,然后创建指向该结构的指针。

either 要么

static struct PipeShm myPipe ={};
static struct PipeShm *pmyPipe = &myPipe;

or 要么

static struct PipeShm *myPipe = malloc();
myPipe->field = value;
static struct PipeShm * myPipe = &(struct PipeShm) {.init = 0 , .flag = FALSE , .mutex = NULL , 
        .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , 
        .semaphoreFlag = FALSE };

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

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