Shared memory in C - getting NULL when I try to access my structure with a char array in it


I'm trying to wrap my mind around the concept of shared memory by making a multiprocessed application that will utilize shared memory and semaphores, but I'm running into an issue with getting the shared memory to work.

The program creates a child which shares a database with the parent. I have an array of structs formatted like this:

struct customer{
    char accName[100];
    float balance;
    int inService;
}

and a shared memory segment initialized like this: (the array needs to be able to hold twenty customers max)

struct customer **shmPtr;
key_t key = 1234;

int shmID;
shmID = shmget(key, sizeof(struct customer)*20, IPC_CREAT | SHM_R | SHM_W); 

shmPtr = shmat(shmID, NULL,0);                                              

I add "customer" structs to the shared memory segment with the following code:

struct customer* newCustomer = malloc(sizeof(struct customer));
strcpy(newCustomer->accName, name);
newCustomer->balance = 100;
newCustomer->inService = 0;

shmPtr[numOfClients] = newCustomer;
numOfClients++;

And for some reason, whenever I try to print the information of the clients from shmPtr (called by the PARENT thread), I'm just getting null stuff. I print it out with the following line:

  struct customer *currCust = shmPtr[i];
        if(currCust == NULL)
        {
            break;
        }
        printf("NAME: %s  ---   Balance: %f  -  Session Status: %d\n", currCust->accName, currCust->balance, currCust->inService); 

And just get a blank space for the %s, 0.0000 for the %f(despite initializing it to 100), and a 0 for %d.

Can anybody point me to what I'm doing wrong? Just when I thought I had memory operations figured out in C, this is throwing another wrench into my mind and I can't figure out what I'm doing wrong as I'm relatively new to the concept of shared memory. I'm guessing it might have something to do with malloc() as the parent can't read stuff that the child has malloc'd, but how would I go about initializing the "Customer" struct within the shared memory?


Answers: