I have an issue that's really confusing me... Below I am calling an initialize function:
void Initialize (List *L) {
char* initialize = "initialize";
int i;
for (i=0; i<MAXLISTSIZE; i++) {
strncpy(L->items[i].name,initialize,MAXNAMESIZE);
L->items[i].name[MAXNAMESIZE - 1] = '\0';
L->items[i].grade = 0;
}
L->count = 0;
}
And it seems to work, I print the values in the loop and it's fine. If I also print inside an identical loop in main to double check it works as well but If I just print the values in main after the initialize function (no print statements in Initialize) I get complete garbage.
It seems the memory I'm storing my values in isn't staying consistent and I can't figure out why.
Do I need to malloc memory for the structs? Since I don't need a variable amount of storage I thought it was not necessary... I am unsure of how to go about that.
My Structs:
typedef Student Item;
#define MAXLISTSIZE 4
typedef struct {
Item items[MAXLISTSIZE];
int count;
} List;
#define MAXNAMESIZE 20
typedef struct {
char name[MAXNAMESIZE];
int grade;
} Student;
Thank you for your time!