@@ -1133,3 +1133,234 @@ using only the memory address of an array.
1133
1133
```
1134
1134
1135
1135
So, pointers are truly essential to be an excellent C programmer.
1136
+
1137
+ # STRUCTURE IN C
1138
+ ## Source Code
1139
+
1140
+ ```
1141
+ #include <stdio.h>
1142
+
1143
+ typedef struct
1144
+ {
1145
+ char *name;
1146
+ int age;
1147
+ float height;
1148
+ float grades[10];
1149
+ } student;
1150
+
1151
+ void printStruct( student oneStudent )
1152
+ {
1153
+ int i;
1154
+
1155
+ printf("=====================\n");
1156
+ printf("Student name: %s\n", oneStudent.name);
1157
+ printf(" age: %d\n", oneStudent.age);
1158
+ printf(" height: %6.3f\n", oneStudent.height);
1159
+ printf(" grades: ");
1160
+ for (i=0; i<10; i++) printf("%4.1f ", oneStudent.grades[i]);
1161
+ printf("\n");
1162
+ printf("=====================\n");
1163
+
1164
+ }
1165
+
1166
+ void modifyStruct( student *oneStudent )
1167
+ {
1168
+
1169
+ oneStudent->grades[0] = 72.0;
1170
+ oneStudent->grades[4] = 81.0;
1171
+ oneStudent->grades[6] = 85.0;
1172
+
1173
+ }
1174
+
1175
+ int main()
1176
+ {
1177
+
1178
+ student Nikki;
1179
+ Nikki.name = "Nikki";
1180
+ Nikki.age = 19;
1181
+ Nikki.height = 64.75;
1182
+ int i;
1183
+ for (i=0; i<10; i++) {
1184
+ Nikki.grades[i] = 98.0;
1185
+ }
1186
+
1187
+ printStruct(Nikki);
1188
+
1189
+ modifyStruct(&Nikki);
1190
+
1191
+ printStruct(Nikki);
1192
+
1193
+ return 0;
1194
+ }
1195
+ ```
1196
+
1197
+ ***
1198
+ ## Result
1199
+
1200
+ ![ result] ( result.png )
1201
+
1202
+ ***
1203
+
1204
+ ## Details
1205
+ ### Structure Data Type
1206
+ This is an example of
1207
+ a structure in C:
1208
+
1209
+ ```
1210
+ typedef struct
1211
+ {
1212
+ char *name;
1213
+ int age;
1214
+ float height;
1215
+ float grades[10];
1216
+ } student;
1217
+ ```
1218
+
1219
+ where ` int age ` is in years,
1220
+ ` float height ` in inches
1221
+ and ` float grades[10] ` are the grades
1222
+ for the 10 assignments.
1223
+
1224
+ Structure is a user-defined
1225
+ data type in C. It is really needed to
1226
+ mix different data types in one declaration.
1227
+
1228
+ #### typedef
1229
+ ` typedef ` is a keyword which will enable
1230
+ you to give an existing type a new name.
1231
+ In our case here, it is just like saying:
1232
+ "Let us define this structure by
1233
+ calling ` student ` ."
1234
+
1235
+ #### Structure Variable
1236
+ ` student ` is a structure
1237
+ variable that can access the members of
1238
+ this structure.
1239
+
1240
+ #### Member Access Operator
1241
+ In order to access a member of an
1242
+ structure we use ` . ` symbol, just like
1243
+ ` oneStudent.name ` .
1244
+
1245
+ ***
1246
+ A function to print the details:
1247
+
1248
+ ```
1249
+ void printStruct( student oneStudent )
1250
+ {
1251
+ int i;
1252
+
1253
+ printf("=====================\n");
1254
+ printf("Student name: %s\n", oneStudent.name);
1255
+ printf(" age: %d\n", oneStudent.age);
1256
+ printf(" height: %6.3f\n", oneStudent.height);
1257
+ printf(" grades: ");
1258
+ for (i=0; i<10; i++) printf("%4.1f ", oneStudent.grades[i]);
1259
+ printf("\n");
1260
+ printf("=====================\n");
1261
+
1262
+ }
1263
+ ```
1264
+
1265
+ Take note of this parameter: ` student oneStudent ` .
1266
+ ` oneStudent ` is a variable of type ` student ` .
1267
+ That is the type we defined in the structure.
1268
+
1269
+ Again, you might be overwhelmed by the ` printf ` function that
1270
+ contains strange things like this: ` %6.3f ` .
1271
+ Don't worry, they are just formatting commands like
1272
+ whether how many decimal places should be there, or
1273
+ you want to pad those numbers with zero.
1274
+
1275
+ In the case of ` %6.3f ` , it means, format the floating
1276
+ type number 6 digit wide, 3 decimal places.
1277
+
1278
+ ***
1279
+ A function to modify the structure:
1280
+
1281
+ ```
1282
+ void modifyStruct( student *oneStudent )
1283
+ {
1284
+
1285
+ oneStudent->grades[0] = 72.0;
1286
+ oneStudent->grades[4] = 81.0;
1287
+ oneStudent->grades[6] = 85.0;
1288
+
1289
+ }
1290
+ ```
1291
+
1292
+ In order to modify the declared structure,
1293
+ we still use pointer as parameter
1294
+ to accept a memory location as argument:
1295
+ that is, to easily locate it and
1296
+ modify everything using the index of each
1297
+ data member.
1298
+
1299
+ Pointers to structs have special ` -> ` symbol
1300
+ for dereferencing the pointer and accessing a
1301
+ data member.
1302
+
1303
+ And what does it do?
1304
+ ` oneStudent->grades[0] = 72.0 ` is equivalent to
1305
+ ` (*oneStudent).grades[0] = 72.0 ` .
1306
+
1307
+ ***
1308
+ In main function, ` student `
1309
+ data type is used: ` student Nikki `
1310
+ is just like any other variable
1311
+ declaration, like ` int myInteger ` .
1312
+ Coming from the structure we defined,
1313
+ we can put data in ` student Nikki ` .
1314
+
1315
+ ```
1316
+ student Nikki;
1317
+ Nikki.name = "Nikki";
1318
+ Nikki.age = 19;
1319
+ Nikki.height = 64.75;
1320
+ int i;
1321
+ for (i=0; i<10 ; i++) {
1322
+ Nikki.grades[i] = 98.0;
1323
+ }
1324
+ ```
1325
+
1326
+ The functions we created, we call them in main function
1327
+ to act upon the structure:
1328
+
1329
+ ```
1330
+ printStruct(Nikki);
1331
+
1332
+ modifyStruct(&Nikki);
1333
+
1334
+ printStruct(Nikki);
1335
+ ```
1336
+
1337
+ We first check whether ` Nikki ` was successfully created by
1338
+ calling the command ` printStruct(Nikki) ` .
1339
+ Then, we modify the initial contents of this structure
1340
+ by calling the command ` modifyStruct(&Nikki) ` . Remember that placing ` & `
1341
+ before a variable will return the address of that variable.
1342
+ And finally, we check again whether the changes were
1343
+ saved by calling the command ` printStruct(Nikki) ` .
1344
+
1345
+ ***
1346
+ There are confusing terms that you might be thinking:
1347
+ data type, database and data structure.
1348
+
1349
+ As was demonstrated, a structure in C is there
1350
+ to mix different data types in one declaration. Every member
1351
+ can be accessed easily. In general, ** data structure** is data that
1352
+ can be accessed piece by piece. For example, an XML file is
1353
+ considered semi-structured, for elements can be parsed but from
1354
+ top to bottom while a database file is a structured data,
1355
+ for pieces of information can be accessed randomly without parsing
1356
+ the entire database.
1357
+
1358
+ ** Data type** is declared to easily tell the compiler
1359
+ how a particular data should be treated.
1360
+
1361
+ ** Database** is an organized
1362
+ collection of data usually expressed as tables.
1363
+ It is commonly built upon a filesystem.
1364
+ Data will persist after the computer is turned off because data
1365
+ resides on persistent storage.
1366
+
0 commit comments