Dynamic array of ints problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viperdriver87
    New Member
    • Jun 2007
    • 5

    Dynamic array of ints problem

    Greetings all, I'm working on a program that contains a struct:

    struct queue {

    int front; //the front of the queue
    int elements; //the number of elements in the queue
    int asize; //the size of the array
    int qlist[]; //an array that will be dynamically sized

    };

    My objective is to read from a file, the length of the queue. Now, I've tried to use malloc, realloc and sorts of things to resize the array qlist[]. All my attempts have failed. My question: how can I resize qlist[] to store a number of ints specified at runtime?

    I'd like to use a pointer to the struct, instead of the struct itself, if possible. Example: struct queue * q, instead of struct queue q;

    Thanks in Advance.
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    #2
    Originally posted by viperdriver87
    Greetings all, I'm working on a program that contains a struct:

    struct queue {

    int front; //the front of the queue
    int elements; //the number of elements in the queue
    int asize; //the size of the array
    int qlist[]; //an array that will be dynamically sized

    };

    My objective is to read from a file, the length of the queue. Now, I've tried to use malloc, realloc and sorts of things to resize the array qlist[]. All my attempts have failed. My question: how can I resize qlist[] to store a number of ints specified at runtime?

    I'd like to use a pointer to the struct, instead of the struct itself, if possible. Example: struct queue * q, instead of struct queue q;

    Thanks in Advance.
    If you know the maximum number of elements that qlist can hold then put that within the brackets. If you don't know then do
    Code:
    int* qlist
    and then you can malloc it.

    Comment

    • viperdriver87
      New Member
      • Jun 2007
      • 5

      #3
      Originally posted by Silent1Mezzo
      If you know the maximum number of elements that qlist can hold then put that within the brackets. If you don't know then do
      Code:
      int* qlist
      and then you can malloc it.
      Ok, that's not a problem. But, say I do malloc it like this:

      struct queue {


      int front;
      int elements;
      int asize;
      int * qlist;

      };

      ...

      struct queue * resize(int size){
      struct queue * q;

      q->front = 0;
      q->elements = 0;
      q->asize = size;
      q->qlist = (int *)malloc(sizeof (int) * size);

      return q;

      }

      in the above function, "size" is the max number of elements i'm going to store. If i attempt to what is shown in the code above, I usually get an 'improper use of flexible member' error. Any ideas?

      Comment

      • Silent1Mezzo
        New Member
        • Feb 2007
        • 208

        #4
        Originally posted by viperdriver87
        Ok, that's not a problem. But, say I do malloc it like this:

        struct queue {


        int front;
        int elements;
        int asize;
        int * qlist;

        };

        ...

        struct queue * resize(int size){
        struct queue * q;

        q->front = 0;
        q->elements = 0;
        q->asize = size;
        q->qlist = (int *)malloc(sizeof (int) * size);

        return q;

        }

        in the above function, "size" is the max number of elements i'm going to store. If i attempt to what is shown in the code above, I usually get an 'improper use of flexible member' error. Any ideas?
        Because q is a pointer to the structure you need to first malloc that pointer so it has enough room to hold everything inside of it
        Code:
        q = malloc(sizeof(struct queue));

        Comment

        • viperdriver87
          New Member
          • Jun 2007
          • 5

          #5
          Brilliant! Problem solved, thanks a bunch!

          Comment

          • Silent1Mezzo
            New Member
            • Feb 2007
            • 208

            #6
            Originally posted by viperdriver87
            Brilliant! Problem solved, thanks a bunch!
            Glad everything worked out for you.

            Comment

            Working...