structures Flashcards

1
Q

Question: What is the most commonly used data structure in C language programs aside from the array?

A

Answer: The struct or structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Question: What is a structure type in C language?

A

Answer: A structure type is a collection of not necessarily identical types used to define a group of variables as a single object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Question: What is the difference between a primitive type and a derived type in C language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Question: What are the primitive types defined in C language?

A

Answer: The primitive types defined in C language are char, int, float, and double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Question: How do you declare an object of int type called noSubjects?

A

Answer: To declare an object of int type called noSubjects, we write “int noSubjects;”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Question: How do you declare a derived type in C language using the struct keyword?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Question: What is an example of a structure type in C language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Question: What is a header file, and what is its purpose in multi-file source code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Question: How do we allocate memory for an object of a structure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Question: What is the purpose of a structure type in C language programs?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Question: What is a derived type in the C language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Question: What is the syntax for declaring a structure type in the C language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Question: What is the purpose of a structure declaration?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Question: What is the purpose of a structure object?

A

Answer: The purpose of a structure object is to allocate memory for the structure so that it can be used in a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Question: What is the role of header files in modularity and organization of code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Question: How do we initialize an object of a structure?

A

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 };

17
Q

Question: How is structure initialization similar to array initialization?

A

Answer: Structure initialization is similar to one of an array.

18
Q

Question: Can we store the declaration of a structure in a separate file called a header file?

A

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).

19
Q

Question: How do we allocate memory for an object of a structure?

A

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;

20
Q

Question: What is the syntax for declaring a structure type?

A

Answer: The syntax for declaring a structure type in the C language takes the form:

struct Tag
{
//… declarations here
};

21
Q

Question: What is the syntax for accessing a member of an object of a structure using dot notation?

A

Answer: The syntax for accessing a member of an object of a structure using dot notation is “object.member”.

22
Q

Question: How do we access the student number of harry?

A

Answer: To access the student number of harry, we write “harry.idNum”.

23
Q

Question: How do we retrieve the address of a non-array member of an object?

A

Answer: To retrieve the address of a non-array member of an object, we use the address of operator “&” and write “&instance.member”.

24
Q

Question: How do we access the address of harry’s student number?

A

Answer: To access the address of harry’s student number, we write “&harry.idNum”.

25
Q

Question: How do we access an array member of an object?

A

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”.

26
Q

Question: How do we access an element of an array member?

A

Answer: To access an element of an array member, we use subscript notation and write “object.member[index]”.

27
Q

Question: How do we access harry’s third grade?

A

Answer: To access harry’s third grade, we write “harry.grade[2]”.

28
Q

Question: How do we retrieve the address of an element of an array member?

A

Answer: To retrieve the address of an element of an array member, we use the address of operator “&” and write “&object.member[index]”.

29
Q

Question: What is the syntax for initializing an object of a structure?

A

Answer: The syntax for initializing an object of a structure is “struct Tag identifier = { value, … , value };”.

30
Q

Question: What is an alternative to parallel arrays for storing tabular information?

A

Answer: An alternative to parallel arrays for storing tabular information is an array of structures.