







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to use structures in c programming language to combine different data types and store complex data. It covers examples of storing student names and grades, retail product information, and scrabble tiles. The document also demonstrates passing structures to functions and updating their values.
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








While an array can hold items of same type, a structure will permit you to combine items of different type. Suppose you wanted to store names of 100 students and grades obtained by them in two exams. You could one array NAME[100] to store all the names, another array GRADE1[100] to store grades obtained in exam1 and finally one more array GRADE2[100] to store the grades of exam2. NAME[100] James Hillary Joan Anil Chen Mendez
..... GRADE1[100] 70 85 56 43 92 80 ..... GRADE2[100] 45 56 67 78 89 12 ..... To process the data using functions, you would have to pass the 3 separate arrays. However, you can use a structure to hold items of different types together. If you declare a structure to hold name, grade1 and grade2, as shown below
struct class { char name[20], int grade1; int grade2; }; then you can declare an array of structures to hold all the items together as one single entity as shown below: struct class CLASS[100]; CLASS[100] James 70 45 Hillary 85 56 Joan 56 67 Anil 43 78 Chen 92 89 Mendez 80 12
.............. Individual items from this structure can be obtained as follows. CLASS[i].name CLASS[i].grade CLASS[i].grade Consider another example from a retail outlet, where one wants to store the name of a product, quantity in stock and its price as one single structure called item. struct item { char name[20], int quantity; double price; }; then to store the information pertaining to 50 products, we can declare an array of structures as follows: struct item ITEMS[50];
printf("%s ", mytiles[i].letter); printf("\n"); } void printScore(struct tile mytiles[], int n) { int i, sum=0; for (i=0; i<n; i++) sum = sum + mytiles[i].score; printf("Score = %d\n", sum); }
We now consider an example where we have a structure for a student in a class. The structure will contain the name of the student and grade obtained in one exam. Then we use this structure to store information for a section of cop3223 having 200 students. We use two functions, one to print the name and grade in an organized way , and the other to print the average grade for the whole class. #include <stdio.h> struct class { char name[20]; int grade; }; void printClasss(struct class cop3223[], int n); void printAverage(struct class cop3223[], int n); int main() { int i; //declare an array of class structure struct class cop3223[200]; // Read in all classs from user. for (i=0; i<200; i++) { printf(“\nenter name and grade for student %d”,i+1); scanf("%s", &cop3223[i].name); scanf("%d", &cop3223[i].grade); }
printClasss(cop3223, 200); printAverage(cop3223, 200); return 0; } void printClasss(struct class cop3223[], int n) { int i; for (i=0; i<n; i++) printf("%s \t %d \n", cop3223[i].name, cop3223[i].grade); printf("\n"); } void printAverage(struct class cop3223[], int n) { int i, sum=0; double av; for (i=0; i<n; i++) sum = sum + cop3223[i].grade; av = sum *1.0/n; printf("average Grade = %.2f\n", av); }
We now consider an example where we have a structure containing two grades for each student and we need to store the average grade for each student in the same structure. In this example we shall also show that we can use different name for the array of structures inside the function, and how the function updates the values in the main. #include <stdio.h> struct class { char name[20]; int grade1; int grade2; double av; };
This example involves colored blocks with a number and a character printed on each block. Given a group of N blocks, the information about each block is obtained from the user. Only one block is updated by the set up function at a time. So we need to pass the name of the block by reference. Then the program compares all pairs of blocks to check if any pair of blocks is identical in color, number and character. #include <stdio.h> struct block { int number; char letter[2]; char color[15]; }; void setup(struct block *b); void print_block(struct block b); int equal(struct block a, struct block b); #define N 5 int main() { int i, j; struct block group[N]; // Initialize all blocks for (i=0; i<N; i++) setup(&group[i]); for (i=0;i< N; i++) print_block(group[i]); // See if any pair are equal for (i=0; i<N; i++) { for (j=i+1; j<N; j++) { if ( equal(group[i],group[j]) ) printf("Blocks %d and %d are identical.\n",i,j); } }
// Get information about one block from the user void setup(struct block *b) { printf("Enter number, letter & color\n"); scanf("%d %s %s", &(b->number), (b->letter), (b->color)); } // Print b's components void print_block(struct block b) { printf("%d %s %s\n", b.number, b.letter, b.color); } // returns 1 if each corresponding component of //blocks a and b are equal, 0 otherwise. int equal(struct block a, struct block b) { if (a.number == b.number && !strcmp(a.letter, b.letter) && !strcmp(a.color, b.color) ) return 1; else return 0; }
void init_hotel(struct hotel h) { int i; for (i=0;i<N;i++) // (h).rooms[i] = 1; h->rooms[i] = 1; h->available = N; } void get_room(struct hotel *h) { int room_no, i; for (i=0; i<N; i++) { if (h->rooms[i] == 1) break; } room_no = i; if(i == N-1) printf("Sorry, we are full.\n"); else { printf("Your room is %d.\n", room_no+1); h->available--; h->rooms[room_no] = 0; } } HANDLING STRUCTURES WITHIN STRUCTURES It is possible to define a structure as a collection of other structures. We shall illustrate this by extending the block example. Let us define another structure called doll. We now combine this with the block structure to declare a toys
structure. Next we operate on the big structure, which calls on functions to operate on its component structures. #include <stdio.h> struct block { int number; char letter[2]; char color[15]; }; struct doll { char name[20]; double price; }; struct toys { struct block rubix; struct doll barbie; }; void setup_block(struct block *b); void print_block(struct block b); void setup_doll(struct doll *d); void print_doll(struct doll d); void init(struct toys *t); void print_all(struct toys t); int main() { struct toys mine; init( &mine ); print_all(mine); return 0; } // Initialize all components of the struct toys points to. void init(struct toys *t) { setup_block( &(t->rubix) ); setup_doll( &(t->barbie) );
It was important to pass the function a memory address, so we did. It was also important to pass the function a memory address to a doll. So, we had to access the doll component of the toy struct. Also, the same care had to be taken for the block component o the toy struct. As this example shows, we can build arbitrarily complex structs, where one struct is part of another, that is part of another etc. It is most important to keep track of which struct is which, and which components each struct is made up of. This way you can always properly access the component you want. Finally, it's important to differentiate between when a dot is needed and when an arrow is needed. Just remember, an arrow is just shorthand for a * preceding a variable with a dot following that variable_. It is completely dependant on whether a variable is a struct itself or a pointer to a struct._