Table of Contents
1 Arrays 1
1.1 Concept of arrays 1
1.1.1 Processing data of the same type 1
1.1.2 Representation of data of the same type 5
1.2 Storage of arrays 6
1.2.1 Definition of arrays 6
1.2.1.1 Definition of arrays 6
1.2.1.2 Reference of array elements 7
1.2.1.3 Storage characteristics of arrays 8
1.2.1.4 Comparison of variables of the same type with plain variables 9
1.2.2 Initialization of arrays 10
1.2.2.1 Initialize all elements 10
1.2.2.2 Initialize some elements 11
1.2.2.3 Array size determined by number of initial values 11
1.2.3 Memory layout of arrays 11
1.2.3.1 Memory layout of one-dimensional arrays 11
1.2.3.2 Memory layout of two-dimensional arrays 11
1.2.4 Memory inspection of arrays 12
1.3 Operations on one-dimensional arrays 16
1.4 Operations on two-dimensional arrays 26
1.5 Operations on character arrays 37
1.6 Summary 45
3.7 Exercises 48
1.7.1 Multiple-choice questions 48
1.7.2 Fill in the tables 50
1.7.3 Programming exercises 51
2 Pointers 53
2.1 Concept of pointers 53
2.1.1 Reference by name and reference by address 53
2.1.2 Management of storage space 55
2.1.2.1 Management of computer memory space 57
2.1.2.2 Storage rules of data in memory 58
2.1.2.3 Address management in memory 58
2.1.3 Definition of pointers 60
2.1.3.1 Comparison of pointer variables and plain variables 60
2.1.3.2 Syntax of pointer definitions 61
2.2 Pointer operations 61
2.2.1 Pointer operators 62
2.2.2 Pointer operations 62
2.2.3 Basic rules of pointer operations 62
2.2.4 Purpose of pointer offsets 67
2.2.4.1 Introduction 67
2.2.4.2 Discussion and conclusion 68
2.2.4.3 Program verification 69
2.2.5 Concept of null pointer 69
2.2.5.1 Meaning of NULL 69
2.2.5.2 Null pointer 71
2.3 Pointers and arrays 72
2.3.1 Pointers and one-dimensional arrays 72
2.3.2 Pointers and two-dimensional arrays 77
2.4 Pointers and multiple strings 82
2.4.1 One-dimensional pointer array and pointer to pointer 84
2.5 Summary 87
2.6 Exercises 87
2.6.1 Multiple-choice questions 87
2.6.2 Fill in the tables 89
2.6.3 Programming exercises 91
3 Composite data 93
3.1 Concept of structures 93
3.1.1 Introduction 93
3.1.2 Storage solution of mixed data table 94
3.1.2.1 Discussion of possible storage solution of mixed data table 94
3.1.2.2 Issues of constructing "combinatorial data" 95
3.1.2.3 Key elements of constructional data 95
3.2 Storage of structures 96
3.2.1 Type definitions of structures 96
3.2.2 Definition of structure variables 98
3.2.3 Structure initialization 99
3.2.4 Memory allocation of structure variables 100
3.2.4.1 Definitions related to structure 100
3.2.4.2 Memory layout of structure variables 100
3.2.4.3 Inspection of memory layout of structure variables 100
3.2.4.4 Data alignment of structures 102
3.2.5 Referencing structure members 105
3.3 Applications of structures 106
3.4 Union 117
3.4.1 Introduction 117
3.4.2 Memory layout of unions 118
3.4.2.1 Union-type definition 118
3.4.2.2 Union variable definition 119
3.4.2.3 Union member reference 119
3.4.2.4 Comparison of unions and structures 120
3.5 Enumeration 124
3.5.1 Introduction 124
3.5.2 Concept and syntax of enumeration 126
3.5.3 Example of enumerations 127
3.5.4 Rules of enumerations 128
3.5.4.1 We cannot assign values of other types to an enumeration variable 129
3.5.4.2 Arithmetic operations are not allowed on enumeration variables 129
3.6 Type definitions 129
3.6.1 Introduction 129
3.6.1.1 Porting of music files 129
3.6.1.2 Cases where macros are not enough 131
3.6.1.3 Define aliases for types 131
3.6.2 Syntax and applications of typedef 131
3.7 Summary 132
3.8 Exercises 134
3.8.1 Multiple-choice questions 134
3.8.2 Fill in the tables 136
3.8.3 Programming exercises 137
4 Functions 139
4.1 Concept of functions 139
4.1.1 Introduction 139
4.1.1.1 Modularization and module reuse in practice 139
4.1.1.2 Abstraction of practical problems: independent code modules 140
4.1.2 Concept of modules 141
4.1.2.1 Coordination problems in teamwork 141
4.1.2.2 Coordination problems in modularization of programs 141
4.1.2.3 Concept of modules 142
4.2 Function form design 143
4.2.1 Methods of communication between modules 143
4.2.2 Function form design 144
4.2.2.1 Analysis of outsourcing structure 144
4.2.2.2 Abstraction of outsourcing structure 145
4.2.2.3 Function form design 145
4.2.2.4 Information transmission mechanism design 146
4.2.2.5 Three syntaxes related to functions 147
4.3 Design of information transmission mechanism between functions 149
4.3.1 Characteristics of information transmission between functions 149
4.3.1.1 Classification of data transmitted between functions 149
4.3.1.2 Expressions of data transmitted between functions 150
4.3.2 Information transmission between functions: submission and receiving of data 150
4.3.2.1 Submission of small amount of data 150
4.3.2.2 Submission of a large amount of data 151
4.3.3 Receiving of function results 153
4.3.3.1 Receiving function results in pass by value 153
4.3.3.2 Receiving function results in pass by reference 153
4.4 Overall function design 153
4.4.1 Key elements of function design 153
4.4.1.1 Key elements of functions 153
4.4.1.2 Relations between function syntax and key elements of function design 154
4.4.2 Summarization of information transmission between functions 154
4.4.2.1 Direction 1: from caller to function 154
4.4.2.2 Direction 2: from function to caller 157
4.4.3 Function call 157
4.4.3.1 Execution and calling order of functions 157
4.4.3.2 Nested call of functions 157
4.4.3.3 Correspondence between parameters and arguments 159
4.4.3.4 Syntax of function call 160
4.5 Examples of function design 160
4.5.1 Call by value 160
4.5.2 Call by reference 168
4.5.3 Comprehensive examples of functions 179
4.5.4 Parameters of the main function 191
4.5.4.1 Introduction 191
4.5.4.2 Parameters of the main function 193
4.5.4.3 Example of the main function with parameters 194
4.6 Scope 196
4.6.1 Introduction 196
4.6.1.1 Cooperation issues in teamwork 196
4.6.1.2 Outsourced projects in a flow 198
4.6.1.3 Resource-sharing problem 198
4.6.2 Masking mechanism of modules 198
4.6.2.1 Isolation of internal data 199
4.6.2.2 Masking rule of functions 199
4.6.3 Memory segments and storage classes 199
4.6.3.1 Memory segments of programs 199
4.6.3.2 Storage classes of variables 200
4.6.4 Masking mechanism 1: lifespan and scope of variables 201
4.6.4.1 Concept of scope 201
4.6.4.2 Attributes of variables 201
4.6.4.3 Local variables and global variables 202
4.6.5 Masking mechanism 2: scope of functions 211
4.6.6 Masking mechanism 3: restriction on shared data 212
4.7 Recursion 215
4.7.1 Case study 215
4.7.2 Concept of recursion 219
4.7.2.1 Definition of recursion 219
4.7.2.2 Type of recursion 220
4.7.2.3 Comparison of recursion and iteration 220
4.7.3 Example of recursion 221
4.8 Summary 224
4.9 Exercises 226
4.9.1 Multiple-choice questions 226
4.9.2 Fill in the tables 229
4.9.3 Programming exercises 233
5 Files: operations on external data 235
5.1 Introduction 235
5.2 Concept of files 236
5.2.1 Binary files 236
5.2.2 Text files 237
5.2.3 File termination mark and end-of-file checking function 237
5.3 Operation flow of files 238
5.4 Data communication between internal and external memory 239
5.5 Operations on files using programs 241
5.5.1 Opening files 241
5.5.2 Reading and writing 243
5.5.3 Closing files 248
5.5.4 Random access 249
5.6 Discussion on file reading and writing functions 251
5.6.1 Case 1: fprintf and fscanf 251
5.6.2 Case 2: fwrite and fread 252
5.6.3 Case 3: fprintf and fscanf 253
5.6.4 Case 4: fwrite and fscanf 254
5.7 Debugging and I/O redirection 254
5.7.1 Code template 1 Using fscanf and fprintf 255
5.7.2 Using freopen function 255
5.7.3 Code template 2 Using freopen function 256
5.8 Summary 257
5.9 Exercises 259
5.9.1 Multiple-choice questions 259
5.9.2 Fill in the tables 261
5.9.3 Programming exercises 262
Appendix A Adding multiple files to a project 265
Appendix B Programming paradigms 273
Appendix C Void type 285
Index 287