I have what should be a relatively simple program (fadec.c) that maps a struct from an included header file (fadec.h) to a shared memory region, but I�m struggling accessing members in the struct from the pointer returned by shmat. Ultimately, I want to access members in the shared memory structure with a globally declared version of the struct, �shm__�. Not only do I not know how to accomplish that, but I can�t even seem to access members of the shared struct directly from the pointer itself (compiler complains about �dereferencing pointer to incomplete type�). I�m at a loss and could use another set of eyes if you guys don�t mind taking a gander:
Compile Errors:
tony-pc:/cygdrive/p/test> cc -o fadec fadec.c
fadec.c: In function 'main':
fadec.c:30: error: dereferencing pointer to incomplete type
fadec.c:31: error: dereferencing pointer to incomplete type
fadec.h:
struct io_struct // sample, full structure is closer to 16384 bytes (see below)
{
int number;
};
fadec.c:
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include "fadec.h"
#define SHM_KEY 1000
struct iostruct *test; // pointer to structure
volatile struct io_struct shm__; // this is ultimately how I want to reference the
// struct in shared memory
int main(int argc, char *argv[])
{
int shmid = shmget(SHM_KEY, 16384, IPC_CREAT);
if( shmid == -1) return -1;
test = shmat(shmid, NULL, 0);
if((int)test == -1)
{
shmctl(shmid, IPC_RMID, NULL);
return -1;
}
printf("Memory attached at %X\n", (int)test);
fprintf(stderr,"test = %X\n",test);
// Need to somehow map 'test' to 'shm__' so that I can access with: shm__.number = 0;
// But, short of that, just try to set a value through 'test' directly:
(* test).number = 0; // or should be able to use �test->number=0;� here
fprintf(stderr,"(* test).number = %d\n",(* test).number);
return(0);
}
Thanks for your help!
Tony
