0

This is a structure that I am using:

struct nodeList//a node structure
{
    int jump;
    int config;
    int level;
    int shifts[200];
    int shift_diff[200];
    struct nodeList *next;
};

I want to create a 2D array of pointers that can be used to reference such a structure variable, ie, any element of that array can be assigned a pointer to a structure variable. I would prefer to create the array dynamically using malloc, if possible. Any pointers (pun unintended) would be appreciated.

SpiderWasp42
  • 2,526
  • 1
  • 12
  • 17

2 Answers2

2

First of all, please think twice about the program design. Do you really need a 2D array of pointers, each pointing to a struct, each struct containing a number of items? Those requirements are rather complex: if you can simplify them, your program will turn out much better.

Because with the current requirements, you'll notice that the pointer and array syntax will turn quite complex, which the requirements are to blame for, more so than the C language.

Consider things like using a 2D array of structs, or to use some sort of pointer-based ADT which makes sense for your given case (linked list, queue, graph, binary tree? etc etc).


That being said, a 2D array of pointers to struct:

struct nodelist* array[X][Y];

To allocate this dynamically, you need a pointer to a 2D array of pointers to struct:

struct nodelist* (*array_ptr)[X][Y];

Then assign this to a 2D array of pointers to struct, allocated dynamically:

array_ptr = malloc( sizeof(struct nodelist*[X][Y]) );

...
free(array_ptr);

Note that unless the 2D array is not allocated like above, in adjacent memory cells, it is not an array.


EDIT. Btw, if you wish to avoid the weird syntax that an array pointer will yield, there is a trick. With the code above you will have to address the array as

(*array_ptr)[i][j];

Meaning: "in the 2D array, give me item number [i][j]".

Had you omitted the inner-most dimension of the type, you could simplify this syntax:

struct nodelist* (*array_ptr)[Y]; // pointer to a 1D array

The malloc will be the same but you can now use it like this instead, which may be more intuitive:

array_ptr[i][j];

Meaning: "in array number i, give me item number j". You are here assuming there is an array of arrays in adjacent memory, which is true.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Note that he'll have to explicitly dereference `array_ptr` to access an element: `(*array_ptr)[i][j] = &some_struct`. It might be better to change that declaration to `struct nodelist *(*array_ptr)[cols]; array_ptr = malloc( rows * sizeof *array_ptr);`. This way he doesn't have to explicitly dereference: `array_ptr[i][j] = &some_struct;`. – John Bode Jun 11 '15 at 11:52
  • @JohnBode Ironically, you were typing that great comment at the very same time as I was already writing an edit suggesting just that :) – Lundin Jun 11 '15 at 11:58
  • Apologies for the convoluted logic. Having to work on somebody else's base code; hence the restrictions. Will pass on the message to the original author. – SpiderWasp42 Jun 11 '15 at 13:12
0
 NODELIST ***pppNode, Node;
 size_t Row = 5, Col = 5, i;

 pppNode = malloc( sizeof(NODELIST **) * Row );

 for(i = 0; i < Row; i++)
      pppNode[i] = malloc( sizeof(NODELIST *) * Col );

 pppNode[1][0] = &Node;

This is another way of dynamic allocation but as @Lundin said if it is not necessary change the design.

Hsyn
  • 46
  • 1
  • 8
  • 1
    This is however, not a 2D array. It is a segmented... thingie. Avoid this style since it will only make the program slower, harder to read & maintain, and fragment the heap. [See this](http://stackoverflow.com/questions/30117161/why-do-i-need-to-use-type-to-point-to-type). In general, avoid three star programming. – Lundin Jun 11 '15 at 12:02