My Program starts Child-Processes with fork to ask the Parent-Process about the UNIX-Time. I've made a Function with a shared memory to write the Ticks (UNIX-Time) and the Client-Number into a struct Array with MAXENTRIES of indexes.
I can't figure out why the values passed to the function f_timeLog(int, int)
don't show in the Array.
If I print with
printf("From: %d\tTICKS: %d\n", logSM[*counter]->vonWem, logSM[*counter]->ticks);
The Values say always: 0. If I debug the program, it writes the first value logSM[0] right, but further not.
Thanks for help!
typedef struct
{
int vonWem;
int ticks;
}timeLog [MAXENTRIES];
void f_timeLog(int who, int ticks)
{
int *counter;
timeLog *logSM;
logSM = (timeLog*) shmat(TimeLog, NULL, (SHM_R|SHM_W));
counter = (int*) shmat(IDCounter, NULL, (SHM_R|SHM_W));
P(SemWriteLog);
logSM[*counter]->vonWem = who;
logSM[*counter]->ticks = ticks;
*counter= *counter+1;
if(*counter >= MAXENTRIES) *counter= *counter - MAXENTRIES;
V(SemWriteLog);
}
The output is logSM[0] vonWem = X, ticks = xxxxxxxx
and logSM[1] and further: vonWem = 0, ticks = 0;
logSM
is a pointer to an array, not an array of pointers.
Because logSM
is a pointer, we need to dereference it. And because it points to an array of structure object (instance) we can't use the "arrow" operator.
So usage should be like
(*logSM)[*counter].ticks = ticks;
A more "natural" solution is to redefine the type-alias timeLog
to be just the structure itself.
So something like
typedef struct
{
int vonWem;
int ticks;
} timeLog;
Then you can use logSM
like any other pointer or array:
logSM[*counter].ticks = ticks;