1

我已经查看了其他问题,但似乎找不到明确的答案。如何在结构内声明结构数组?我试图在 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;
}

建议后编辑:好的,现在我遇到了问题。我不断收到此错误:

3 [main] TurnBasedSystem 47792 open_stackdumpfile: Dumping stack trace to TurnBasedSystem.exe.stackdump

我不得不稍微修改一下代码,让所有的东西都变成指针。我对其进行了测试,一旦我尝试分配其属性之一,它就会给我该错误,例如: bq->head = 0

整个事情只是应该将一张卡片添加到队列中。修改后的代码如下:

#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;
}
4

3 回答 3

3

bq.cards是结构数组,您不必这样做malloc。您可以将整个数组初始化为:

memset(bq.cards, 0, sizeof(bq.cards));

如果你想初始化bq

    memset(&bq, 0, sizeof(bq));
于 2013-06-23T08:09:51.130 回答
1

您可能希望以这种方式初始化整个结构:

...

int main(int argc, char *argv[])
{
  struct battleQ bq =
  {
    {
      { 0 } /* int priority; */
    } /* (initialising the first element/member initialises all element/member) */
  }; 

  //...

  return 1;
}
于 2013-06-23T08:17:04.087 回答
0

初始化在声明中

struct battleQ bq = {
              {{0}, {0},... // 20 times
              },
                  0,
                  0,
                  0
            };

将卡片作为最后一个元素可能会更好,然后您可以使用称为 C 的 chumminess 的技巧。http://c-faq.com/struct/structhack.html,您可以在其中拥有可变大小的battleQ。

我可能错了,但我在大多数编译器上发现的是,如果将第一个元素设置为零,那么其他所有元素都将为零。我记得在标准中读过一些关于它的东西,但我不记得是什么,也不记得它是 C 还是 C++。

struct battleQ bq = {{{0}}};
于 2013-06-23T08:17:23.253 回答