I have been trying to get pointers to work with a structure that I am trying to implement on a PIC. I have tried the same code on a Linux compiler and it works fine. I have also tried stripping it back to basics, but to no avail. Also, I have tried this without pointers and it works.
Here is the code I am using:
struct circular_buf{
size_t tail;
size_t head;
size_t max;
};
struct circular_buf * cbuf;
And within the main
cbuf = malloc(sizeof(struct circular_buf));
cbuf->tail = 22;
cbuf->head = 45;
cbuf->max =99;
When I print this out it only prints max out correctly. However, when I insert items to separate the max, tail, and head in the structure it works fine.
struct circular_buf{
size_t spacer1; // seperatating the items so it reads correctly
size_t tail;
size_t spacer2; // seperatating the items so it reads correctly
size_t head;
size_t spacer3; // seperatating the items so it reads correctly
size_t max;
};
Why is this and is there anything I can do to correct this memory error without doing this?