0
votes

I'm getting this error:

error: incompatible types when assigning to type ‘struct sharedMem’ from type ‘void *’

when trying to mmap a struct to sharedmemory. Here's what I'm trying to do:

//struct for each card
typedef struct card{
    char c[3]; //card
    char s; //suit
} card;

//struct with player information
typedef struct player{
    int num;
    char* nickname;
    char* FIFO_P;
    struct card* hand;
} player;

//struct for sharedMemory
typedef struct sharedMem{
    unsigned int nplayers;
    unsigned int dealer;
    struct player *players;
    unsigned int roundnumber;
    unsigned int turn;
    struct card *tablecards;
} sharedMem;

then I have a function that does:

int createSharedMemory(char* shmname,int nplayers){

    int shmfd;
    char SHMNAME[100]={'\0'};
    char *ps;
    ps=&SHMNAME[0];
    strcat(ps,"/");
    strcat(ps,shmname); // name = /shmname
    shmfd = shm_open(SHMNAME,O_RDWR,0755);
    if (shmfd<0){
        if (errno==2){ //File or directory does not exist (shared memory space not created, meaning it's the first process, create the SHMSpace)
            shmfd = shm_open(SHMNAME,O_CREAT|O_RDWR,0755);
            if (shmfd<0){
                perror("Failure in shm_open()");
                fprintf(stderr,"Error: %d\n",errno);
                exit(EXIT_FAILURE);             
            }
        } else {
            perror("Failure in shm_open()");
            fprintf(stderr,"Error: %d\n",errno);
            exit(EXIT_FAILURE);
        }
    }

    struct sharedMem shm;
//  shm.players=malloc(nplayers*sizeof(player));
//  shm.tablecards=malloc(nplayers*sizeof(card));
    ftruncate(shmfd, sizeof(sharedMem));

error: shm = mmap(0,sizeof(sharedMem),PROT_READ|PROT_WRITE,MAP_SHARED,shmfd,0);

    return 0;

}

Can someone tell me what I'm doing wrong? I thought attributes players and tablecards had to be allocated so I tried a malloc but no cigar.

2

2 Answers

0
votes

You want a pointer to struct sharedMem:

struct sharedMem *shmem = mmap(...);

Time to fire up the docs of mmap().

0
votes
void *mmap(void *addr, size_t len, int prot, int flags,
       int fildes, off_t off);

mmap is returning a void pointer and you are trying to collect it in structure.So make a struct sharedMem* and typecast mmap return in (struct sharedMem*) before assigning it to sharedMem*.