









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
An overview of structures and unions in c programming, including their declaration, initialization, functions, arrays, and the sizeof operator. It also includes sample programs and examples of using structures and unions.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Deitel & Deitel - C++ How to Program Chapter 6, 16 6.2, 6.3, 6.4, 16.2, 16.3, 16.4, 16.
Today, we will discuss the concepts of structures and unions which are very interesting part of C language. These are also in C++. After dilating upon structures, we will move to the concept of classes, quite similar to ‘structures’. What a structure is? We can understand ‘structure’ with the example of students of a class discussed in some of the earlier lectures. Suppose we have data about students of a class i.e. name, addresses, date of birth, GPA and courses of study. This information is related to only a single entity i.e. student. To understand the matter further, we can think of a car with its specifications like model, manufacturer company, number of seats and so on. But there is always a requirement in most of our data processing applications that the relevant data should be grouped and handled as a group. This is what the concept of structure is. In structure, we introduce a new data type. In the previous lectures, we had been dealing with int, float, double and char in our programs. You are fully familiar with the term ’strings’ but there is no data type called strings. We have used ‘array of char’ as strings. While dealing with numbers, there is no built-in mechanism to handle the complex numbers. This means that there is no data type like complex. The FORTRAN language (Formula Translation) written for scientific application, has a complex data type. Therefore, in FORTRAN, we can say complex x; now x is a variable of type complex and has a real part and an imaginary part. There is no complex data type in C and C++. While trying to solve the quadratic equation on the similar grounds, we may have a complex number as answer i.e. if we have to calculate the square root of -1, an iota () will be used. So the
combination of real and imaginary parts is called complex number. In C, C++ we deal with such situations with structures. So a structure is not simply a grouping of real world data like students, car etc, it also has mathematical usage like complex number. The definition of structure is as under:
“A structure is a collection of variables under a single name. These variables can be of different types, and each has a name that is used to select it from the structure”
Declaration of a Structure: Structures are syntactically defined with the word struct. So struct is another keyword that cannot be used as variable name. Followed by the name of the structure. The data, contained in the structure, is defined in the curly braces. All the variables that we have been using can be part of structure. For example:
struct student{ char name[60]; char address[100]; float GPA; };
Here we have a declared a structure, ‘student’ containing different elements. The name of the student is declared as char array. For the address, we have declared an array of hundred characters. To store the GPA, we defined it as float variable type. The variables which are part of structure are called data members i.e. name, address and GPA are data members of student. Now this is a new data type which can be written as:
student std1, std2;
Here std1 and std2 are variables of type student like int x, y; x and y in this case are variables of int data type. This shows the power of C and C++ language and their extensibility. Moreover, it means that we can create new data types depending upon the requirements. Structures may also be defined at the time of declaration in the following manner:
struct student{ char name[60]; char address[100]; float GPA; }std1, std2;
We can give the variable names after the closing curly brace of structure declaration. These variables are in a comma-separated list.
Structures can also contain pointers which also fall under the category of data type. So we can have a pointer to something as a part of a structure. We can’t have the same structure within itself but can have other structures. Let’s say we have a structure of an address. It contains streetAddress like 34 muslim town, city like sukhar, rawalpindi, etc and country like Pakistan. It can be written in C language as:
docsity.com
There are also limitation with structures as we can not say card1 + card2; As the operator plus (+) does not know how to add two structures. We will learn to overcome these limitations at the advanced stage. On the other hand, assignment of structures works. Therefore if s1 and s2 are of type student structure, we can say that s1 = s. The assignment works because the structure is identical. So the name will be copied to the name , address to address and so on. If we want to display the structure with cout , it will also work. The cout is a very intelligent function as it interprets the structure besides showing the output.
Initializing Structures We have so far learnt how to define a structure and declare its variables. Let’s see how can we put the values in its data members. The following example can help us understand the phenomenon further.
struct student{ char name[64]; char course[128]; int age; int year; };
student s1, s2, s3;
Once the structure is defined, the variables of that structure type can be declared. Initialization may take place at the time of declaration i.e.
student s1 = {“Ali”, “CS201”, 19, 2002 };
In the above statement, we have declared a variable s1 of data type student structure and initialize its data member. The values of data members of s1 are comma separated in curly braces. “Ali” will be assigned to name , “CS201” will be assigned to the course , 19 to age and 2002 to year. So far we have not touched these data members directly.
To access the data members of structure, dot operator (.) is used. Therefore while manipulating name of s1 , we will say s1.name. This is a way of referring to a data member of a structure. This may be written as:
s1.age = 20; s1.year = 2002;
The above statement will assign the value 20 to the age data member of structure s. Can we assign a string to the name of s1? Write programs to see how to do this? You may need string copy function to do this. Also, initialize the pointers to structure and see what is the difference.
Similarly, to get the output of data members on the screen, we use dot operator. To display the name of s1 we can write it as:
cout << “The name of s1 = “ << s1.name;
docsity.com
Other data members can be displayed on the screen in the same fashion.
Here is a simple example showing the initialization and displaying the structure. /* Simple program showing the initialization of structure.*/
#include <iostream.h>
main() { // Declaring student structure struct student{ char name[64]; char course[128]; int age; int year; }; // Initializing the structure student s1 = {"Ali", "CS201- Introduction to programming", 22, 2002};
cout << "Displaying the structure data members" << endl; cout << "The name is " << s1.name << endl; cout << "The course is " << s1.course << endl; cout << "The age is " << s1.age << endl; cout << "The year is " << s1.year << endl; }
The output of the above program is: Displaying the structure data members The name is Ali The course is CS201- Introduction to programming The age is 22 The year is 2002
Here, s1 is a unit. The data members have been grouped together. If we have s1 and s2 as two variables of student type and want to copy the data of s1 to s2 , it can be written as:
s2 = s1;
Functions and structures We can pass structures to functions. Structures are passed into functions as per the C/C++ calling conventions by value. In other words, a copy of entire structure is put on the stack. The function is called which removes it from the stack and uses the structure. We can also pass the structures by reference to function. This can be performed in the same way we do with the normal variables i.e. pass the address of the structure to the function. This is call by reference.
docsity.com
// Assigning a structure to pointer sptr = &s1;
cout << "Displaying the structure data members using pointers" << endl; cout << "Using the * operator" << endl; cout << endl; cout << "The name is " << (sptr).name << endl; cout << "The course is " << (sptr).course << endl; cout << "The age is " << (sptr).age << endl; cout << "The year is " << (sptr).year << endl; cout << endl;
cout << "Using the -> operator" << endl; cout << endl; cout << "The name is " << sptr->name << endl; cout << "The course is " << sptr->course << endl; cout << "The age is " << sptr->age << endl; cout << "The year is " << sptr->year << endl; }
The output of the program is: Displaying the structure data members using pointers Using the * operator
The name is Ali The course is CS201- Introduction to programming The age is 22 The year is 2002
Using the -> operator
The name is Ali The course is CS201- Introduction to programming The age is 22 The year is 2002
Arrays of structures Let’s discuss the arrays of structure. The declaration is similar as used to deal with the simple variables. The declaration of array of hundred students is as follows:
student s[100];
In the above statement, s is an array of type student structure. The size of the array is hundred and the index will be from 0 to 99. If we have to access the name of first student, the first element of the array will be as under:
s[0].name;
docsity.com
Here s is the array so the index belongs to s. Therefore the first student is s[0], the 2nd student is s[1] and so on. To access the data members of the structure, the dot operator is used. Remember that the array index is used with the array name and not with the data member of the structure.
Sizeof operator As discussed earlier, the sizeof operator is used to determine the size of data type. The sizeof operator can also be used with the structure. Structure contains different data types. How can we determine its size in the memory? Consider the student structure that contains two char arrays and two int data types. We can simply use the sizeof operator to determine its size. It will tell us how many bytes the structure is occupying.
sizeof(s1);
We don’t need to add the size of all the data members of the structure. This operator is very useful while using the write() function to write the structure in the file.
Here is a small example which shows the number of bytes a structure occupies in memory. /* this program shows the memory size of a structure*/
#include <iostream.h>
main() { // Declaring student structure struct student{ char name[64]; char course[128]; int age; int year; };
student s1 = {"Ali", "CS201- Introduction to programming", 22, 2002}; // using sizeof operator to determine the size cout << "The structure s1 occupies " << sizeof(s1) << " bytes in the memory"; }
The output of the above program is:
The structure s1 occupies 200 bytes in the memory
Let’s summarize what we can do with structures:
We can define the structure We can declare variables of that type of structure We can declare pointers to structure
docsity.com
cout << "Enter the Student's Course : " ; cin >> students[i].course ; cout << "Enter the Student's Age : " ; cin >> students[i].age ; cout << "Enter the Student's GPA : " ; cin >> students[i].GPA ; }
maxGPA = 0; // Calculating the total age, total GPA and max GPA for ( int j = 0; j < noOfStudents; j++ ) { totalAge = totalAge + students[j].age ; totalGPA = totalGPA + students[j].GPA ;
// Determining the max GPA and storing its index if ( students[j].GPA > maxGPA ) { maxGPA = students[j].GPA; index = j; } } // Calculating the average age averageAge = totalAge / noOfStudents ; cout << "\n The average age is : " << averageAge << endl;
// Calculating the average GPA averageGPA = totalGPA / noOfStudents ; cout << "\n The average GPA is : " << averageGPA << endl; cout << "\n Student with max GPA is : " << students[index].name << endl ;
// Determining the Grade of the class if (averageGPA > 4) { cout << "\n Wrong grades have been enter" << endl ; } else if ( averageGPA == 4) { cout << "\n The average Grade of the class is : A" << endl; } else if ( averageGPA >= 3) { cout << "\n The average Grade of the class is : B" << endl; } else if ( averageGPA >= 2) { cout << "\n The average Grade of the class is : C" << endl; } else {
docsity.com
cout << "\n The average Grade of the class is : F" << endl; } }
The output of the program with three students i.e. when noOfStudents = 3
Enter data for Student # : 1 Enter the Student's Name : Ali Enter the Student's Course : CS Enter the Student's Age : 24 Enter the Student's GPA : 3.
Enter data for Student # : 2 Enter the Student's Name : Faisal Enter the Student's Course : CS Enter the Student's Age : 22 Enter the Student's GPA : 3.
Enter data for Student # : 3 Enter the Student's Name : Jamil Enter the Student's Course : CS Enter the Student's Age : 25 Enter the Student's GPA : 3.
The average age is : 24
The average GPA is : 3.
Student with max GPA is : Faisal
The average Grade of the class is : B
Sample Program 2 Problem: Read the student data from a file, populate the structure and write the structure in another file. Solution: We have to read from a file. We will write a function which will read from a file and return a structure to the calling program. The prototype of function is:
returnType functionName (argument list)
As the function is returning a student structure so the return type will be ‘ student’. We can name the function as getData() as it is reading from a file a returning the data (i.e. student structure). In the arguments, we can give it the handle of the file from which the data is to be read. For the simplicity, we keep the argument list empty. Therefore, the prototype of our function is as under:
docsity.com
#include <fstream.h>
// Global variables for input and output files ifstream inFile; ofstream outFile;
//student structure struct student { char name[30]; char course[15]; int age; float GPA; };
// function declarations void openFile(); // open the input and output files student getData(); // Read the data from the file void writeData(student); // write the structure into a file
const int noOfStudents = 3; // Total no of students openFile(); // opening input and output files student students[noOfStudents]; // array of students
// Reading the data from the file and populating the array for(int i = 0; i < noOfStudents; i++) { if (!inFile.eof()) { students[i] = getData(); } else { break ; } }
// Writing the structures to the file for(int i = 0; i < noOfStudents; i++) { writeData(students[i]); }
// Closing the input and output files inFile.close ( ) ; outFile.close ( ) ; }
main()
docsity.com
/* This function opens the input file and output file */ void openFile() { inFile.open("SAMPLE.TXT", ios::in); inFile.seekg(0L, ios::beg); outFile.open("SAMPLEOUT.TXT", ios::out | ios::app); outFile.seekp(0L, ios::end);
if(!inFile || !outFile) { cout << "Error in opening the file" << endl; exit(1); } }
/* This function reads from the file */ student getData() { student tempStudent; // temp variables for reading the data from file char tempAge[2]; char tempGPA[5];
// Reading a line from the file and assigning to the variables inFile.getline(tempStudent.name, '\n'); inFile.getline(tempStudent.course, '\n'); inFile.getline(tempAge, '\n'); tempStudent.age = atoi(tempAge); inFile.getline(tempGPA, '\n'); tempStudent.GPA = atof(tempGPA);
// Returning the tempStudent structure return tempStudent; }
/* This function writes into the file the student structure*/ void writeData(student writeStudent) { outFile << writeStudent.name << endl; outFile << writeStudent.course << endl; outFile << writeStudent.age << endl; outFile << writeStudent.GPA << endl; }
The contents of output file is:
nasir CS 23 3
docsity.com
cout << “ The value in ival = “ << uval.ival;
It will print “The value in ival = 10”. Now what will be output of the following statement?
cout << “ The value in dval = “ << uval.dval;
We don’t know. The reason is that in the eight bytes of double, integer is written somewhere. When we use integer, it is printed fine. When we printed the double, the value of int will not be displayed. Rather something else will be printed. Similarly in the following statement i.e.
uval.dval = 100.0; cout << “ The value in dval = “ << uval.dval;
It will print the right value of dval. The value of this double is written in such a way that it will not be interpreted by the integer. If we try to print out ival , it will not display 100. Unions are little bit safer for integer and characters. But we have to think in terms that where to store the value in memory.
Suppose, we have some integer value 123 and want to append 456 to it so that it becomes 123456. How can we do that? To obtain this result, we have to shift the integer three decimal places i.e. we can multiply the integer 123 by 1000 (i.e. 123000) and then add 456 to it (i.e. 123456). Consider a union containing four characters and an integer. Now the size of the char is one and integer is four so the size of the union will be four. We assign the character ‘a’ to the integer, and display the chars and integer value. If we want to shift the value of first byte into the second byte, the integer will be multiplied by 256(i.e. A byte contains 8 bits and 2 to power 8 is 256), then add character ‘b’ to it. We see that the char variables of union contains ‘a’ and ‘b’.
Here is the code of the program; /* This program uses a union of int and char and display the memory usage by both */ #include <iostream.h>
main() { // Declaration of union union intOrChar{ char c[4]; int x; }u1;
u1.x = 'a'; // Assigning ‘a’ to x // Displaying the char array and integer value cout << "The value of c = " << u1.c[0] << "," << u1.c[1] << "," << u1.c[2] << "," << u1.c[3]<< endl; cout << "The value of x = " << u1.x << endl;
// Shifting the values one byte and adding ‘b’ to the int
docsity.com
u1.x *= 256; u1.x += 'b';
// Displaying the char array and integer value cout << "The value of c = " << u1.c[0] << "," << u1.c[1] << "," << u1.c[2] << "," << u1.c[3]<< endl; cout << "The value of x = " << u1.x << endl;
// Shifting the values one byte and adding ‘b’ to the int u1.x *= 256; u1.x += 'c';
// Displaying the char array and integer value cout << "The value of c = " << u1.c[0] << "," << u1.c[1] << "," << u1.c[2] << "," << u1.c[3]<< endl; cout << "The value of x = " << u1.x << endl;
// Shifting the values one byte and adding ‘b’ to the int u1.x *= 256; u1.x += 'd';
// Displaying the char array and integer value cout << "The value of c = " << u1.c[0] << "," << u1.c[1] << "," << u1.c[2] << "," << u1.c[3]<< endl; cout << "The value of x = " << u1.x << endl; }
The output of the program is;
The value of c = a, , , The value of x = 97 The value of c = b,a, , The value of x = 24930 The value of c = c,b,a, The value of x = 6382179 The value of c = d,c,b,a The value of x = 1633837924
Unions are very rarely used. They become very important when we want to do some super efficient programming. Experiment with the unions and structures.
We have learnt how to use structures and unions. These are relatively less used parts of C/C++ language. But structures at least are very useful. They allow us a convenient way of grouping data about a single entity. We have used student entity in our example. You can think of a car or any other object and find out its properties before grouping them in a structure. We don’t need to manipulate its properties individually as grouping them into a unit is a better option. Try to write different programs using structures.
docsity.com