Made with ScholarlyMake flashcards like these from your own notes
Question:What is a structure in programming?
Answer:A user-defined data type that combines a limited number of data elements into a unified data unit.
Question:What is an example of a structure?
Answer:A record about a book in a library with properties such as title, author, subject, book ID, and publishing year.
Question:How do you declare a struct?
Answer:By specifying its data elements in arbitrary types using the struct keyword: struct Book { char* title; char* author; char* subject; char* ID; int year; };.
Question:How do you declare a struct variable?
Answer:By specifying the type of the struct and the name of the variable: struct Book fiction1;.
Question:How do you initialize a struct?
Answer:By assigning values to its data elements using curly braces: struct Book fiction1 = {"Harry Potter", "JKRowling", "fiction","Fi123", 2005};.
Question:How do you access a member of a struct?
Answer:Using the dot . operator: fiction1.title = "Harry Potter";.
Question:What is the typedef keyword used for?
Answer:To give a shorter name for the struct, making it easier to declare variables: typedef struct Book bk; bk fiction1;.
Question:How do you calculate the size of a struct?
Answer:By using the sizeof operator: printf("Size of book struct is %lu\n", sizeof(Book));.
Question:Can struct variables be compared?
Answer:No.
Question:Can members of struct variables be compared?
Answer:Yes.
Question:How do you declare an array of structs?
Answer:By specifying the type of the struct and the size of the array: Book fiction[5];.
Question:How do you access elements of an array of structs?
Answer:By using the dot . operator and the index of the element: fiction[1].title = "The Lord of the Rings";.