Thread: Expand a 2D char array inside a Struct

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    1

    Expand a 2D char array inside a Struct

    Hi Guys,

    I am trying to expand the 2d array elements but is not working as intent.

    Code:
    typedef struct exec_orders_t
    {
        char                 id[37];
        char                 id2[37];
        char                 date[33];
        int                 queue_size;
        char **            queue[1][100];
    } exec_orders_t;
    so in the main part I do as follow

    Code:
    app_main(){
         struct exec_orders_t * exec_orders;
    
    
         exec_orders->queue= realloc(exec_orders->queue,sizeof(char *)*100*5);
    }
    but that is always error, I tried to change it to a pointer but give error as well.

    Can you guys give an advice on what is the error on this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char ** queue[1][100];
    I'm going to assume that what you want is something line char queue[N][100] for some variable N.

    Because 2 stars and 2 brackets is a 4-D thing.


    Begin with
    char (*queue)[100];

    Then you can do
    Code:
    void *temp = realloc(exec_orders->queue, sizeof(*exec_orders->queue)*(exec_orders->queue_size + 10) );
    if ( temp ) {
        exec_orders->queue = temp;
        exec_orders->queue_size += 10;
    }
    When using realloc, always go via a temporary pointer initially.
    If realloc does return NULL, it does NOT free the previous memory. You need your pointer to remain valid until you know realloc was successful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-22-2016, 10:45 AM
  2. appcrash segmentation fault char* inside struct
    By lenaReoma in forum C Programming
    Replies: 2
    Last Post: 04-24-2013, 02:45 PM
  3. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  4. array inside struct
    By tat in forum C Programming
    Replies: 15
    Last Post: 11-13-2007, 12:36 PM
  5. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM

Tags for this Thread