Made with ScholarlyMake flashcards like these from your own notes
Question:What is a structure?
Answer:A user-defined data type that combines a limited number of data elements into a unified data unit.
Question:What is the syntax for declaring a struct?
Answer:struct Book { char* title; char* author; char* subject; char* ID; int year; };
Question:How do you declare a struct variable?
Answer:struct Book fiction1;
Question:How do you initialize a struct?
Answer:Declare the variable and assign values to each attribute, e.g. struct Book fiction1 = { "Harry Potter", "JKRowling", "fiction", "Fi123", 2005 };
Question:How do you access a member of a struct?
Answer:Use the "." operator, e.g. fiction1.title
Question:What is the purpose of typedef with structs?
Answer:To give a shorter name for the struct, e.g. typedef struct Book bk; bk fiction1; or typedef struct { char* title; char* author; char* subject; char* ID; int year; } Book; Book fiction2;
Question:How do you declare an array of structs?
Answer:Book fiction[5];
Question:What is the size of a struct?
Answer:The size of a struct can be found using the sizeof() function, e.g. printf("Size of book struct is %lu\n", sizeof(Book));