By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,482 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,482 IT Pros & Developers. It's quick & easy.

Block of pointers

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.