[OFF-TOPIC] C question

Mario Lobo lobo at bsd.com.br
Wed Apr 5 16:22:19 UTC 2017


Hi There !

I don't know if this list is appropriate for this. 

if it isn't, please point me to the right direction.

Since this is for a Freebsd program, I decided to start here, hoping to
find some C gurus who can help me out.

Here it goes:

1) I have this;

typedef struct {
   ulong me;
   ulong record;
   char  string[256];

}KFNODE;

KFNODE ***Nodes;

2) Then allocate for the pointers;

Nodes = (KFNODE ***) malloc(5 * sizeof(KFNODE **));

memset( Nodes, 0x00, (5 * sizeof(KFNODE **)));  

for(a = 0; a < 5; a++) {
    Nodes[a] = (KFNODE **) malloc(43 * sizeof(KFNODE *));
    memset( Nodes[a], 0x00, (43 * sizeof(KFNODE *)));
}

3) Allocate for the structs

for(a = 0; a < 5; a++) {
    for(b = 0; b < 43; b++) { 
        Nodes[a][b] = (KFNODE *) malloc(sizeof(KFNODE));
        memset( Nodes[a][b], 0x00, sizeof(KFNODE));
    }
}



From this point on, I can access any Nodes[a<5][b<43]. This works fine
as long as I previously know is a 2D array.

My question is: The dimensions are not known before hand and have to be
found at run time.

Suppose I find out I'll need a 3D array, like Nodes[a][b][c]?

How can I dynamically change the level of indirection?


How can I dynamically switch:

----------------
KFNODE ***Nodes; to  KFNODE ****Nodes;
----------------
Nodes = (KFNODE ***) malloc(5 * sizeof(KFNODE **));
to Nodes = (KFNODE ****) malloc(5 * sizeof(KFNODE ***));
----------------

and so forth?

Is this possible at all?

Thanks

-- 
Mario Lobo
http://www.mallavoodoo.com.br
FreeBSD since 2.2.8 [not Pro-Audio.... YET!!]
 
"UNIX was not designed to stop you from doing stupid things, 
because that would also stop you from doing clever things."


More information about the freebsd-hackers mailing list