[英]Initializing a struct array inside a struct
I've looked at other questions but I can't seem to find a clear answer for this. 我看过其他问题,但似乎找不到明确的答案。 How do I declare a struct array inside of a struct?
如何在结构内部声明结构数组? I attempted to do it in main() but I don't know if I'm doing it right and I keep getting this warning: "initialization makes integer from a pointer without cast"
我试图在main()中执行此操作,但是我不知道自己执行的是否正确,并且不断收到以下警告:“初始化从没有强制转换的指针生成整数”
#define MAXCARDS 20
struct card {
int priority;
};
struct battleQ {
struct card cards[MAXCARDS];
int head;
int tail;
int size;
};
int main (int argc, char *argv[]) {
struct battleQ bq;
bq.cards = {
malloc(MAXCARDS * sizeof (struct card)), //Trouble with this part
0,
0,
0
};
//...
return 1;
}
Edit after suggestions: Okay now I'm having problems. 根据建议进行编辑:好的,现在我遇到了问题。 I keep getting this error:
我不断收到此错误:
3 [main] TurnBasedSystem 47792 open_stackdumpfile: Dumping stack trace to TurnBasedSystem.exe.stackdump
I had to change the code a bit and make everything pointers. 我不得不稍微修改一下代码并使所有指针成为指针。 I tested it and it gives me that error as soon as I try to assign one of its attributes like: bq->head = 0
我对其进行了测试,并在尝试分配其属性之一(例如bq-> head = 0)时给了我该错误。
The entire thing is just supposed to add a card to a queue. 整个过程仅应将卡添加到队列中。 Revised code is below:
修改后的代码如下:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define MAXCARDS 20
struct card {
int priority;
};
// A queue defined by a circular array
struct battleQ {
struct card *cards[MAXCARDS];
int head;
int tail;
int size;
};
bool battleQEnqueue (struct battleQ *bq, struct card *c);
bool battleQisFull(struct battleQ *bq);
// Method for enqueuing a card to the queue
bool battleQEnqueue (struct battleQ *bq, struct card *c) {
bool success = false;
if (battleQisFull(&bq)) {
printf("Error: Battle queue is full\n");
} else {
success = true;
bq->cards[bq->tail] = c;
bq->size = bq->size + 1;
bq->tail = (bq->tail + 1) % MAXCARDS;
}
return success;
}
int main (int argc, char *argv[]) {
int i;
struct battleQ *bq;
memset(&bq, 0, sizeof(bq)); // Did I do this properly?
bq->tail = 0; // Gives error at this point
bq->head = 0;
bq->size = 0;
// This is where I create a card and add it to the queue but the main problem
// is still the initialization above
for (i = 0; i < 5; i++) {
struct card *c = malloc(sizeof(c));
c->priority = i + 10;
printf("%d,", c->priority);
battleQEnqueue(&bq, &c);
}
return 1;
}
bq.cards
is array of structs, you don't have to malloc
it. bq.cards
是结构数组,您不必对其进行malloc
。 You can initialize the entire array as: 您可以将整个数组初始化为:
memset(bq.cards, 0, sizeof(bq.cards));
If you want to initialize bq
do 如果要初始化
bq
请执行
memset(&bq, 0, sizeof(bq));
You might like to initialise the whole structure this way: 您可能希望以这种方式初始化整个结构:
...
int main(int argc, char *argv[])
{
struct battleQ bq =
{
{
{ 0 } /* int priority; */
} /* (initialising the first element/member initialises all element/member) */
};
//...
return 1;
}
The initialization is in the declaration 初始化在声明中
struct battleQ bq = {
{{0}, {0},... // 20 times
},
0,
0,
0
};
It might be better to have cards as the last element, then you can use a trick called the chumminess of C. http://c-faq.com/struct/structhack.html , where you can have a variable sized battleQ. 这可能是最好有卡作为最后一个元素,那么你可以使用一个名为C的亲密关系伎俩http://c-faq.com/struct/structhack.html ,在那里你可以有一个可变大小battleQ。
I may be wrong on this but what I've found on most compilers is that if you set the first element to zero, everything else will be zero. 我对此可能是错的,但是我在大多数编译器中发现的是,如果将第一个元素设置为零,则其他所有内容都将为零。 I remember reading something in the standard about it but I can't remember were or whether it was C or C++.
我记得在标准中读过有关它的内容,但我不记得它是C还是C ++。
struct battleQ bq = {{{0}}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.