Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Apr 2017 13:23:16 -0300
From:      Mario Lobo <lobo@bsd.com.br>
To:        freebsd-questions@freebsd.org
Cc:        FreeBSD Hackers <freebsd-hackers@freebsd.org>
Subject:   [OFF-TOPIC] C question
Message-ID:  <20170405132251.536ab064@Papi>

next in thread | raw e-mail | index | archive | help
Hi There !

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

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 =3D (KFNODE ***) malloc(5 * sizeof(KFNODE **));

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

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

3) Allocate for the structs

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



=46rom 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 =3D (KFNODE ***) malloc(5 * sizeof(KFNODE **));
to Nodes =3D (KFNODE ****) malloc(5 * sizeof(KFNODE ***));
----------------

and so forth?

Is this possible at all?

Thanks

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20170405132251.536ab064>