【发布时间】:2019-10-16 15:33:06
【问题描述】:
我定义了一个这样的结构:
struct Profile {
char name[15]; // Name of profile for displaying to screen later
int numberOfPulses; // Number of pulses that will occur for one Profile run
int lengthOfPulse; // Length (uSec) of each pulse produced
int timeBetweenPulses; // Time (uSec) between consecutive pulse outputs
}
然后,我创建了许多这种结构类型的对象,将每个对象存储在一个适当的变量中。
struct Profile defaultP = {"Default", 1, 100, 0};
struct Profile repeat3 = {"Repeat_3", 3, 100, 2000};
struct Profile shortPulse = {"Short_50us", 1, 50, 0};
struct Profile shortPulseRepeat3 = {"Sh_50_R3", 3, 50, 2000};
struct Profile longPulse = {"Single_300", 1, 300, 0};
struct Profile longPulseRepeat3 = {"Sin_300_R3", 3, 300, 2000};
struct Profile custom = {"Custom", 1, 100, 0};
最后,我尝试将这些变量分配给一个数组,这就是我遇到许多错误的地方,包括类型不匹配和/或重新定义等。
struct Profile profileArray[] = {defaultP, repeat3, shortPulse, shortPulseRepeat3, longPulse, longPulseRepeat3, custom};
我真的不明白我做错了什么,但我缺乏经验,并认为这是某种指针问题。 一个重要的注意事项是我必须像我一样单独定义我的 struct Profile 变量,因为它们稍后会在程序中被调用。 谢谢。
【问题讨论】:
-
请张贴实际错误的样本。
-
另外请显示此代码的上下文(即它是全局的吗?在函数内部?两者都有?)。
-
除此之外,您需要在结构定义的末尾使用分号。应该是
struct Profile { .... };
标签: c arrays pointers struct initialization