2013-06-23 107 views
0

我已经看了其他问题,但似乎无法找到明确的答案。我如何在结构体中声明结构体数组?我试图做它在main(),但我不知道如果我这样做是正确和我不断收到这样的警告:“初始化将整数指针不投”初始化struct中的结构数组

#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; 
} 
+0

顺便说一下,故障可能是由引起的malloc返回一个void * –

回答

3

bq.cards是结构的数组,你不必malloc它。

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

如果你想初始化bq

memset(&bq, 0, sizeof(bq)); 
+0

这应该是:'memset的(BQ,0,BQ的sizeof)'中有机磷农药的情况。 (没有所有'bq.cards = {...}' – Lekensteyn

+0

@Lekensteyn,我不确定OP是否想要从语句的左值初始化'bd'或'bq.cards',它看起来像'bq.cards '。 – Rohan

+0

@Rohan看起来像'bq',因为他将其他成员设置为'0'。 – Lekensteyn

1

你可能想初始化整个结构是这样的::

... 

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

    //... 

    return 1; 
} 
0

初始化 ,你可以初始化整个数组在申报中

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

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

我可能是错误的,但我发现在大多数编译器上是,如果你设置第一个元素为零,其他的都是零。我记得阅读关于它的标准的东西,但我不记得是或是否是C或C++。

struct battleQ bq = {{{0}}}; 
+0

如果每个数组元素都为0,则不需要为每个数组元素指定一个初始化程序,'struct battleQ bq = {{{0}},0,0,0};'就够了。你甚至不需要那么多,'struct battleQ bq = {0};'也一样。而不是结构黑客应该使用标准的方式,一个灵活的数组成员。 –

+0

是的,我意识到在写完之后我会纠正它,但是我被要求由更高权威机构去做其他事情。哈哈。没关系。 – cup