0

Following my previous post , I want to take that one step ahead :

I want to allocate a shared memory region , and put initial values for the allocated/shared data :

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

int shmid  = shmget(IPC_PRIVATE, sizeof(int), 0600);
static struct PipeShm  * myPipe = shmat(shmid, NULL, 0); // &myPipeSt;

myPipe = & myPipeSt; // that doesn't compile 

Suggestions ?

Much appreciated !

Community
  • 1
  • 1
JAN
  • 21,236
  • 66
  • 181
  • 318

2 Answers2

4

First of all you only ask for shared memory for the size of an integer, not for the whole structure. Even if it's rounded up to the nearest page size, you should always use proper size of the structure you are going to use.

Secondly, to copy from one structure to another, you simply assign. To copy to a pointer to the structure, you have to use the dereferencing operator *, like:

*myPipe = myPipeSt;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

This does compile:

*myPipe = myPipeSt;

You can copy structure objects with the simple assignment operator and for myPipe as it is a pointer you need to dereference it to access the structure object.

ouah
  • 142,963
  • 15
  • 272
  • 331