Hello,
Programmers say it is better to define arrays of structs in a function or in main instead of defining them at the top of a .c file having global scope to the file.
So here it is ... which way is better or which way would you guys do this...
Would you guys prefer to do this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//================================================ Common/h
typedef struct myX{
int a;
} MYX;
//================================================= Main.c
typedef struct myStruct ms;
struct myStruct{
MYX MXScore1[10];
MYX MXScore2[10];
MYX MXScore3[10];
};
ms* newStruct()
{
ms* pTables;
pTables = (ms*) malloc(sizeof(struct myStruct));
pTables->MXScore1[0].a = 11;
pTables->MXScore1[1].a = 16;
pTables->MXScore1[2].a = 17;
pTables->MXScore1[3].a = 12;
pTables->MXScore1[4].a = 13;
pTables->MXScore1[5].a = 131;
pTables->MXScore1[6].a = 15;
pTables->MXScore1[7].a = 17;
pTables->MXScore1[8].a = 19;
pTables->MXScore1[9].a = 111;
pTables->MXScore2[0].a = 11;
pTables->MXScore2[1].a = 16;
pTables->MXScore2[2].a = 17;
pTables->MXScore2[3].a = 12;
pTables->MXScore2[4].a = 13;
pTables->MXScore2[5].a = 131;
pTables->MXScore2[6].a = 15;
pTables->MXScore2[7].a = 17;
pTables->MXScore2[8].a = 19;
pTables->MXScore2[9].a = 111;
pTables->MXScore3[0].a = 11;
pTables->MXScore3[1].a = 16;
pTables->MXScore3[2].a = 17;
pTables->MXScore3[3].a = 12;
pTables->MXScore3[4].a = 13;
pTables->MXScore3[5].a = 131;
pTables->MXScore3[6].a = 15;
pTables->MXScore3[7].a = 17;
pTables->MXScore3[8].a = 19;
pTables->MXScore3[9].a = 111;
return pTables;
}
int main(void)
{
ms* m;
int w;
m = newStruct();
w = m->MXScore1[0].a;
free(m);
return 0;
}
OR THIS:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//================================================ Common/h
typedef struct myX{
int a;
} MYX;
//================================================= Main.c
MYX MXScore1[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
MYX MXScore2[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
MYX MXScore3[10] = {11, 16, 17, 12, 13, 131, 15, 17, 19, 111};
typedef struct myStruct ms;
struct myStruct{
MYX* MXScores;
};
ms* newStruct()
{
ms* pTables;
pTables = (ms*) malloc(sizeof(struct myStruct));
pTables->MXScores = MXScore1;
pTables->MXScores = MXScore2;
pTables->MXScores = MXScore3;
return pTables;
}
int main(void)
{
ms* m;
int w;
m = newStruct();
w = m->MXScores[0].a;
free(m);
return 0;
}
All feedbacks appreciated!
PS... please bear in mind that there may be 100 or so of these tables and that they may be 50 or even 100 items as opposed to 10!!!!
r