【发布时间】:2016-12-09 01:20:51
【问题描述】:
我正在为一个 Arduino 爱好项目做恶作剧,但我遇到了一个我无法解决的情况。
基本上,我有一个指向 Thing 的全局(因为 Arduino)指针数组。我不想在声明期间对其进行初始化,但我想利用好的数组初始化语法。
请注意,这是一个 Arduino 项目,因此使用了不寻常的 C 和 C++ 组合,因为 Arduino。 从一些基础研究来看,如果它可以在 C 或 C++ 中工作,看起来像这样没有库,它可以解决这个问题。
注意:我使用 NULL 作为一种在循环遍历数组时检测数组结尾的方法,在代码的其他地方。
类似这样的:
struct Thing {
int i;
};
Thing * MakeThing() {
return new Thing;
}
Thing * things[] = {
NULL
};
void setup() {
things = (Thing*[]){
MakeThing(),
NULL
};
}
但是,这让我得到了error: incompatible types in assignment of 'Thing* [2]' to 'Thing* [1]'
我试过了:
Thing* _things[] {
MakeThing(),
NULL
};
things = _things;
同样的错误。
我试过了:
things = new Thing*[] {
MakeThing(),
NULL
};
同样的错误。
我试过了:
things = new Thing** {
MakeThing(),
NULL
};
得到error: cannot convert '<brace-enclosed initializer list>' to 'Thing**' in initialization
我试过了:
things = new Thing*[] {
MakeThing(),
NULL
};
得到error: incompatible types in assignment of 'Thing**' to 'Thing* [1]'
这是怎么回事?我怎样才能让它发挥作用?
【问题讨论】:
-
注意:C 中没有
new运算符。 -
我要把标签从
c改成c++ -
您不能分配数组。我不认为 C++ 有复合文字。
-
@chqrlie 重新标记不正确。这是一个 Arduino 项目,AFAIK 仅是 C。我的 C 足够模糊,以至于我认为
new是其中的一个东西。 -
我使用
new重新标记并删除了该示例。感谢您提供帮助。
标签: c arrays pointers arduino initialization