1

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"

#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

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;
}
1
  • by the way, the trouble is probably caused by malloc returning a void* Commented Jun 23, 2013 at 8:12

3 Answers 3

3

bq.cards is array of structs, you don't have to malloc it. You can initialize the entire array as:

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

If you want to initialize bq do

    memset(&bq, 0, sizeof(bq));
Sign up to request clarification or add additional context in comments.

6 Comments

This should be: memset(&bq, 0, sizeof bq) in OPs case. (without all the bq.cards = { ... }.
@Lekensteyn, I'm not sure OP wants to initiazlise bd or bq.cards from the lvalue of statement it looks like bq.cards.
@Rohan Looks like bq to me since he sets the other members to 0 as well.
@Ron, As bq is pointer now, you have to allocate memory bq=malloc(sizeof(*bq)) and memset(bq, 0, sizeof(*bq)) or rather use calloc().
@Rohan Thanks that works. But when I use the same thing to intialize cards[], it gives me that same error. I did: bq->cards = malloc(sizeof(*(bq->cards))); It says incompatible types when assigning struct card*[20] from type *void.
|
1

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

Comments

0

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.

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++.

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

2 Comments

You don't need to specify an initializer for each array element if they all shall be 0, struct battleQ bq = { {{0}}, 0, 0, 0 }; is enough. You don't even need that much, struct battleQ bq = {0}; does the same. And instead of the struct hack one should use the standard way, a flexible array member.
Yes, I realized that after I wrote it and was going to correct it but I was asked to do something else by a higher authority. Ha ha. Never mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.