Block of pointers

N

Nikos Mitas

I have this structure
struct DIRECTORY{

char nameDir[40];
struct DIRECTORY *parent;
int numSubDir;
struct DIRECTORY *subDir[10];
};

and i allocate it with malloc.Is it possible instead of using an array
of pointers to dynamically allocate a block containing the pointers,so
that if it fills i can realloc it to bigger block?How can this be done?

i tried something like this but doesnt work:
struct DIRECTORY{

char nameDir[40];
struct DIRECTORY *parent;
int numSubDir;
int block;
struct DIRECTORY *subDir[block];
};

Thanks
 
T

TIT

Nikos Mitas sade:
I have this structure
struct DIRECTORY{

char nameDir[40];
struct DIRECTORY *parent;
int numSubDir;
struct DIRECTORY *subDir[10];
};

and i allocate it with malloc.Is it possible instead of using an array
of pointers to dynamically allocate a block containing the pointers,so
that if it fills i can realloc it to bigger block?How can this be done?

i tried something like this but doesnt work:
struct DIRECTORY{

char nameDir[40];
struct DIRECTORY *parent;
int numSubDir;
int block;
struct DIRECTORY *subDir[block];
};

Thanks

struct DIRECTORY {
char nameDir[40];
struct DIRECTORY *parent;
int numSubDir;
struct DIRECTORY *subDir;
};

#include <stdlib.h>

int main(int argc, char* argv[])
{
struct DIRECTORY d;
d.numSubDir = 10;
d.subDir = malloc(sizeof(struct DIRECTORY) * d.numSubDir);
d.subDir[0].nameDir[0] = 'A';
/* ... */

/* oops out of space */
d.numSubDir += 8;
d.subDir = realloc(d.subDir, sizeof(struct DIRECTORY) * d.numSubDir);

/* now an additional 8 DIRECTORYs exist in the array */
d.subDir[12].nameDir[0] = 'B';

free(d.subDir);

return 0;
}

TIT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top