gpt4 book ai didi

c - 用于使用指向结构的指针循环结构成员

转载 作者:行者123 更新时间:2023-12-02 09:07:58 24 4
gpt4 key购买 nike

我认为我的指针游戏生锈了,我似乎无法让 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 的指针。您不会在循环的展开 版本中这样做,对吗?

循环的其他组件没问题。

关于c - 用于使用指向结构的指针循环结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com