Reputation: 320
I think my pointer game is rusty, I can't seem to get the for loop implementation to work. It's like the pointer isn't incremented along with the array. Any suggestions?
I implemented a "manual" version of what i want the loop to do. It worked as expected.
typedef struct txstruct_t
{
uint8_t tx[8];
void(*send)(struct txstruct_t *cthis, uint8_t arr[8]);
}txstruct_t;
void send(txstruct_t *cthis, uint8_t arr[8]);
void loop()
{
txstruct_t txobj;
uint8_t buf[8] = { 1, 0, 1, 0, 1, 0, 1, 0 };
send(&txobj, buf);
}
// This works
void send(txstruct_t *cthis, uint8_t arr[8])
{
cthis->tx[0] = arr[0];
cthis->tx[1] = arr[1];
cthis->tx[2] = arr[2];
cthis->tx[3] = arr[3];
cthis->tx[4] = arr[4];
cthis->tx[5] = arr[5];
cthis->tx[6] = arr[6];
cthis->tx[7] = arr[7];
return;
};
/*
//This doesn't work :(
void send(txstruct_t *cthis, uint8_t arr[8])
{
for (int i = 0; i < 8; i++)
{
cthis = cthis + i;
cthis->tx[i] = arr[i];
}
return;
};*/
I would love some clarification on this, so I could learn how to avoid this in the future! Maybe even a better method of implementing such buffers.
Upvotes: 2
Views: 69
Reputation: 234725
cthis = cthis + i;
can be removed. It makes no sense at all as it's an undefined attempt to somehow reset the pointer to the struct
. You don't do that in the unrolled version of the loop, do you?
The other components of the loop are fine.
Upvotes: 5