32

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 3
    You need to allocate memory to the pointer so that you can do anything meaningful with it. A pointer by itself just points to an random address.You need to make sure that the address pointed by the pointer is big enough to hold the structure contents. – Alok Save Jul 29 '12 at 14:19
  • I don't get it... why do you need a *pointer* ? – Karoly Horvath Jul 29 '12 at 14:21
  • See question [here](http://stackoverflow.com/questions/2177391/allocating-memory-for-a-structure-in-c) In order to declare a pointer you need to allocate memory so your basically asking the same thing. – rudolph9 Jul 29 '12 at 14:23

7 Answers7

58

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • +1. Didn't know about this. Where does the memory come from in this case? – Jay Jul 29 '12 at 14:29
  • 2
    @Jay *The storage for this object is either static (if the compound literal occurs at file scope) or automatic (if the compound literal occurs at block scope), and the storage duration is associated with its immediate enclosing block* – cnicutar Jul 29 '12 at 14:31
  • @cnicutar: Lovely answer +1 & chosen . – JAN Jul 29 '12 at 14:34
  • if defined in a statement block, we do not need to free the struct pointer, right? – NewBee May 12 '19 at 02:05
5

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;
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
4

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;
JAN
  • 21,236
  • 66
  • 181
  • 318
2

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.

Jay
  • 24,173
  • 25
  • 93
  • 141
1

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

struct PipeShm * pMyPipe = &myPipe;
oseiskar
  • 3,282
  • 2
  • 22
  • 22
0

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;
Jokester
  • 5,501
  • 3
  • 31
  • 39
0
static struct PipeShm * myPipe = &(struct PipeShm) {.init = 0 , .flag = FALSE , .mutex = NULL , 
        .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , 
        .semaphoreFlag = FALSE };
mousetail
  • 7,009
  • 4
  • 25
  • 45