[英]For looping struct members with a pointer to a struct
我認為我的指針游戲生銹了,我似乎無法使for循環實現正常工作。 就像指針沒有隨數組增加一樣。 有什么建議么?
我實現了希望循環執行的“手動”版本。 它按預期工作。
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;
};*/
我希望對此有所澄清,以便將來可以避免這種情況! 甚至可能是實現此類緩沖區的更好方法。
cthis = cthis + i;
可以刪除。 這根本沒有任何意義,因為這是一次未定義的嘗試,以某種方式將指針重置為struct
。 您不在循環的展開版本中這樣做,對嗎?
循環的其他組件也不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.