如何在结构中创建节点数组。 我上传了我的样本。
struct timebasedSpecificTimesIntervalNode
{
int hrs;
int min;
int sec;
};
struct timebasedSpecificTimesInterval
{
struct timebasedSpecificTimesIntervalNode* nodes;
int count;
char *cFilePath;
};
如何为此结构创建节点数组timebasedSpecificTimesInterval。
struct timebasedSpecificTimesInterval specificTimes;
如何为此结构创建3个节点的数组。
修改
为此值创建结构
hrs:5,2,3 min 23,58,4 sec 54,12,2
Thnks
答案 0 :(得分:2)
int main(void) {
struct timebasedSpecificTimesInterval data;
data.count = 3;
data.nodes = malloc(data.count * sizeof *data.nodes);
data.cFilePath = NULL;
if (data.nodes) {
data.nodes[0].hrs = 5; data.nodes[0].min = 23; data.nodes[0].sec = 54;
data.nodes[1].hrs = 2; data.nodes[1].min = 58; data.nodes[1].sec = 12;
data.nodes[2].hrs = 3; data.nodes[2].min = 4; data.nodes[2].sec = 2;
/* use data */
free(data.nodes);
data.nodes = NULL; /* optional */
data.count = 0;
}
return 0;
}
编辑:使用OP中提供的示例
答案 1 :(得分:0)
我就是这样做的。除非我有完全错误的结束。我不喜欢乱丢我代码的新struct关键字。
typedef struct
{
int hrs;
int min;
int sec;
} timebasedSpecificTimesIntervalNode;
typedef struct
{
timebasedSpecificTimesIntervalNode* nodes;
int count;
char *cFilePath;
} timebasedSpecificTimesInterval;
int main (void)
{
timebasedSpecificTimesIntervalNode nodeArray[3];
timebasedSpecificTimesInterval specificTimesInterval;
//initialise the pointer
specificTimesInterval.nodes = nodeArray;
// you can now access the pointer as an array
nodeArray[0].hrs = 3; //arbitrary value
}
答案 2 :(得分:-1)
除非我误解了这个问题......就像你对任何其他阵列一样:
struct timebasedSpecificTimesInterval specificTimes[3];