JAN
JAN

Reputation: 21855

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 = {.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?

Upvotes: 32

Views: 48984

Answers (7)

user20787710
user20787710

Reputation: 11

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

Upvotes: 1

cnicutar
cnicutar

Reputation: 182609

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.


Regarding the storage of compound literals:

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.

Upvotes: 62

Jokester
Jokester

Reputation: 5617

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;

Upvotes: 0

JAN
JAN

Reputation: 21855

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;

Upvotes: 4

Brian Cain
Brian Cain

Reputation: 14619

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;

Upvotes: 5

oseiskar
oseiskar

Reputation: 3372

First initialize the struct (static struct PipeShm myPipe = {...). Then take the address

struct PipeShm * pMyPipe = &myPipe;

Upvotes: 1

Jay
Jay

Reputation: 24895

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.

Upvotes: 3

Related Questions