How to initialize a pointer to a struct in C?

前端 未结 6 1856
礼貌的吻别
礼貌的吻别 2020-12-02 17:27

Given this struct:

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


        
6条回答
  •  情深已故
    2020-12-02 18:11

    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;
    

提交回复
热议问题