Re: Dinamically allocate array of array of structures



valerio posted:

Hello all
I would like to dinamically allocate an array of array of structures.


I.e. a two-dimensional array of structures.


To explain this:

struct file{ char* fileName,int inode) myfiles[];
struct file{ char* fileName,int inode) mydirs[][];


Is this your first day learning C? Get the syntax right.

The amount of files in each directory will have to be constant:

#define FILES_PER_DIR 5
#define QUANTITY_DIRS 2

#include <stdlib.h>

typedef struct File {
char const *name;
} File;

File dir1[] = {{"a.exe"},{"b.exe"},{"c.exe"},{"d.exe"},{"e.exe"}};
File dir2[] = {{"a.bat"},{"b.bat"},{"c.bat"},{"d.bat"},{"e.bat"}};

File mydirs[QUANTITY_DIRS][FILES_PER_DIR];

int main(void)
{
unsigned i,j;
memcpy(mydirs,dir1,sizeof dir1);
memcpy(mydirs+1,dir2,sizeof dir2);

for(i=0; i!=QUANTITY_DIRS; ++i)
{
for(j=0; j!=FILES_PER_DIR; ++j)
{
puts(mydirs[i][j].name);
}
}

return 0;
}

--

Frederick Gotham
.



Relevant Pages

  • Re: Array of Array refs
    ... Learning how to parse your problem should be your first step ... push() works perfectly well. ... elements to an array. ... Then you didn't delcare your variables in the correct scope. ...
    (perl.beginners)
  • Re: sort array
    ... >...And yes, I'm learning for an exam,. ... >after fixing the i-1 thing, doing the cards trick, algorithm stil ... which can be smaller then the array. ... >what do I have to fix in order to make it sort? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Adding values to a combo box
    ... I'm still learning the in's and out's of Excel ... the combo box is on Sheet1 and the array is on Sheet2. ... only had success in my efforts when I put the coding to populate the ... Sub Main ...
    (microsoft.public.excel.programming)
  • Re: splitting.............
    ... or how can i make an array of name which contains all the name. ... just learning & learning. ... Srikanth Jeeva wrote: ... if this is not the format u needed, specify what output u need. ...
    (comp.lang.ruby)
  • Adding values to a combo box
    ... I'm still learning the in's and out's of Excel ... the combo box is on Sheet1 and the array is on Sheet2. ... only had success in my efforts when I put the coding to populate the ... Sub Main ...
    (microsoft.public.excel.programming)