gpt4 book ai didi

清理 C 代码中的指针

转载 作者:行者123 更新时间:2023-11-30 14:22:20 27 4
gpt4 key购买 nike

在下面的代码片段中,可以做些什么来a)让编译器安静,b)清理交叉的指针困惑?

extern struct tree *sintablein[sintablesize];
struct tree *(*tablein)[];
int i;

tablein = &sintablein; // The compiler complains:
// "redundant & applied to array (warning)" and
// "illegal conversion between pointer types (warning)"

for(i = 0; i < 10; i++) {
struct tree *tablemember = (*tablein)[i]; // However, this works like a charm.
// Do stuff with tablemember
}

我能够走到这一步的唯一方法是使用非常有帮助的 http://cdecl.org/ 。特别是关于(b),如何尽可能简化指针和取消引用?

最佳答案

我会这样做:

extern struct tree *sintablein[sintablesize];
struct tree **tablein = sintablein;

// ...

for(i = 0; i < 10; i++) {
struct tree *tablemember = tablein[i];
// Do stuff with tablemember
}

请注意,当您执行 (*tablein)[i] 时,您正在获取 tablein 数组的第一个元素并将其视为 struct 数组树项而不是单个struct tree*指针。这可能不是您想要的。

关于清理 C 代码中的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811276/

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