I have to switch two elements (structures) in a data structure composed by an array. When I create a new element for the data structure, I maintain a pointer to that element, so I can modify it later. When I try to change the value, it seems to be the same as before. Can you tell me where I'm wrong?
struct TimeData {
struct timeval time_send;
struct timeval time_recv;
struct timeval timeout;
struct timeval time_stamp;
int seq;
};
struct TimerWheel {
struct CircularBuffer *cb;
};
struct TimeData *newTimeData(int seq, time_t sec, suseconds_t usec) {
struct TimeData *td;
td = malloc(sizeof(struct TimeData));
td->seq = seq;
td->timeout.tv_sec = sec;
td->timeout.tv_usec = usec;
gettimeofday(&td->time_send, NULL);
return td;
}
int timerWheelAdd(struct TimerWheel *tw, struct TimeData *td) {
if (circularBufferIsFull(tw->cb) == 1)
return 1;
else {
circularBufferAdd(tw->cb, td);
circularBufferShiftE(tw->cb);
return 0;
}
}
struct TimeData *timerWheelGetTmr(struct TimerWheel *tw) {
if (circularBufferIsEmpty(tw->cb) == 1)
return NULL;
else {
struct TimeData *td;
td = circularBufferRead(tw->cb);
circularBufferShiftS(tw->cb);
return td;
}
}
int main() {
struct TimeData *td5;
struct TimeData *td6;
td5 = newTimeData(1, 3, 5000);
td6 = newTimeData(2, 5, 6000);
struct TimerWheel *tw1 = newTimerWheel(10);
timerWheelAdd(tw1, td5);
timerWheelAdd(tw1, td6);
///////NOW I TRY TO MODIFY td5
td5->seq = 67;
struct TimeData *temp;
while ((temp = timerWheelGetTmr(tw1)) != NULL)
printf("%d\n", temp->seq);
//////td5->seq is the same as before
}
EDIT
The CircularBuffer struct is only a generic circular buffer of (void *
) element. This data structure works fine. The problem is this:
why I cannot change an integer in a struct when I have a pointer to that struct?
This is my CircularBuffwer: C : Insert/get element in/from void array