I'm still fairly new to programming in C, and still learning the language. I'm trying to do the following (its a scaled down version of my initial program code). But every time I try to initialize counter, the program crashes. I've tried setting up another function to handle the initialization process, setting up the counter to be incremented in the set_members function, and tried initializing the counter by passing it the address of total &total. But every time I try to run my program, it crashes after printing "Hello". I think I'm trying to initialize counter incorrectly because I don't fully understand pointers and structs in C. Can someone explain how or why my initialization process is incorrect, and causes my program to crash? And how I can do it correctly going forward? Thanks!
typedef struct CharStruct{
char *names;
}CharStruct;
typedef struct Count{
CharStruct *arr;
int counter;
}Count;
typedef struct Members{
Count *member;
}Members;
typedef struct Test{
Members people;
}Test;
void set_members(struct Test *person);
void print_total(struct Test *person);
int main(void) {
printf("Hello\n"); /* prints Hello */
Test person;
//person.people.member->counter = 0;
set_members(&person);
print_total(&person);
system("pause");
return EXIT_SUCCESS;
}
void set_members(struct Test *person)
{
int total = 0;
while(total < 10)
{
++total;
}
person->people.member->counter = total;
}
void print_total(struct Test *person)
{
printf("Total Members: %d\n", person->people.member->counter);
}
whileloop inset_members, why are you doing this?Testcontains aMembersstruct,Memberscontains a pointer toCountwhich contains a pointer ofCharStruct? What problem are you trying to solve?