I have an array of structs I'm trying to get into shared memory. This is what I have so far.
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
typedef struct
{
int num;
int denom;
} Fraction;
Fraction FracArray[5];
int main()
{
key_t key;
int shmid;
key = 5678;
shmid = shmget(key, 1024, IPC_CREAT | 0666)
FracArray[5] = shmat(shmid, NULL, 0);
return 0;
}
When compiled I get
incompatible types when assigning to type ‘Fraction’ from type ‘void *’
So while trying to find a solution I saw someone mention to try and use pointers and stick that into shared memory so I came up with
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
typedef struct
{
int num;
int denom;
} Fraction;
Fraction *FracArray[5];
int main()
{
key_t key;
int shmid;
key = 5678;
shmid = shmget(key, 1024, IPC_CREAT | 0666);
FracArray[5] = shmat(shmid, NULL, 0);
printf("test\n");
FracArray[1]->num = 5;
return 0;
}
But when I run that program test prints fine but then it seg faults. Can anyone explain which way I should be doing it and how to actually get the struct array into shared memory correctly?