structures Flashcards
Question: What is the most commonly used data structure in C language programs aside from the array?
Answer: The struct or structure.
Question: What is a structure type in C language?
Answer: A structure type is a collection of not necessarily identical types used to define a group of variables as a single object.
Question: What is the difference between a primitive type and a derived type in C language?
Answer: A primitive type is a core language-defined type that cannot be redefined or introduced. On the other hand, a derived type is a collection of other types that can be declared using the struct keyword.
Question: What are the primitive types defined in C language?
Answer: The primitive types defined in C language are char, int, float, and double.
Question: How do you declare an object of int type called noSubjects?
Answer: To declare an object of int type called noSubjects, we write “int noSubjects;”.
Question: How do you declare a derived type in C language using the struct keyword?
Answer: The declaration of a derived type in C language using the struct keyword takes the form “struct Tag { //… declarations here };”, where Tag is the name by which we call the structure.
Question: What is an example of a structure type in C language?
Answer: An example of a structure type in C language is the Student struct, which consists of the student’s ID number and the student’s grades (up to 4 individual grades). Its declaration takes the form “struct Student { int idNum; float grade[4]; };” and does not allocate any memory for any object.
Question: What is a header file, and what is its purpose in multi-file source code?
Answer: A header file is a separate file that contains the declaration of a structure (or other code). Its purpose is to allow the code to be written in one spot and edited in that one spot alone. In multi-file source code, the header file’s code is inserted into the source file that requires that information.
Question: How do we allocate memory for an object of a structure?
Answer: To allocate memory for an object of a structure, we define it using the form “struct Tag identifier;”, where “Tag” is the name of the structure and “identifier” is the name of the object.
Question: What is the purpose of a structure type in C language programs?
Answer: The purpose of a structure type is to define a group of variables as a single object. A structure type is a collection of not necessarily identical types.
Question: What is a derived type in the C language?
Answer: In the C language, a derived type is a collection of other types. One example of a derived type is the struct or structure.
Question: What is the syntax for declaring a structure type in the C language?
Answer: The syntax for declaring a structure type in the C language is “struct Tag { [type] [identifier]; // … other types };”, where “Tag” is the name of the structure, “type” is the member’s type, and “identifier” is the name by which we access the member’s value.
Question: What is the purpose of a structure declaration?
Answer: The purpose of a structure declaration is to define the structure and the rules for objects of that type. It does not allocate any memory for any object.
Question: What is the purpose of a structure object?
Answer: The purpose of a structure object is to allocate memory for the structure so that it can be used in a program.
Question: What is the role of header files in modularity and organization of code?
Answer: Header files play an important role in modularity and organization of code. They often contain additional things like macros and function prototypes which help organize code.
Question: How do we initialize an object of a structure?
Answer: To initialize an object of a structure, we add a braces-enclosed, comma-separated list of values. We organize the initial values in the same order as the member listing in the declaration of the structure. The initialization takes the form:
struct Tag identifier = { value, … , value };
Question: How is structure initialization similar to array initialization?
Answer: Structure initialization is similar to one of an array.
Question: Can we store the declaration of a structure in a separate file called a header file?
Answer: Yes, we can store the declaration of a structure in a separate file called a header file. We declare our structure globally and may store its declaration in a separate file called a header file (say, with the name Student.h).
Question: How do we allocate memory for an object of a structure?
Answer: To allocate memory for an object of a structure, we define an object of a structure with the following syntax:
struct Tag identifier;
struct Student harry;
Question: What is the syntax for declaring a structure type?
Answer: The syntax for declaring a structure type in the C language takes the form:
struct Tag
{
//… declarations here
};
Question: What is the syntax for accessing a member of an object of a structure using dot notation?
Answer: The syntax for accessing a member of an object of a structure using dot notation is “object.member”.
Question: How do we access the student number of harry?
Answer: To access the student number of harry, we write “harry.idNum”.
Question: How do we retrieve the address of a non-array member of an object?
Answer: To retrieve the address of a non-array member of an object, we use the address of operator “&” and write “&instance.member”.
Question: How do we access the address of harry’s student number?
Answer: To access the address of harry’s student number, we write “&harry.idNum”.
Question: How do we access an array member of an object?
Answer: To access an array member of an object, we refer to its name without brackets. For example, to access harry’s grades, we write “harry.grade”.
Question: How do we access an element of an array member?
Answer: To access an element of an array member, we use subscript notation and write “object.member[index]”.
Question: How do we access harry’s third grade?
Answer: To access harry’s third grade, we write “harry.grade[2]”.
Question: How do we retrieve the address of an element of an array member?
Answer: To retrieve the address of an element of an array member, we use the address of operator “&” and write “&object.member[index]”.
Question: What is the syntax for initializing an object of a structure?
Answer: The syntax for initializing an object of a structure is “struct Tag identifier = { value, … , value };”.
Question: What is an alternative to parallel arrays for storing tabular information?
Answer: An alternative to parallel arrays for storing tabular information is an array of structures.