www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 37: Structures Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
In this lesson, we will discuss the concept of structures which is a very interesting part of C language. We can understand ‘structure’ with the example of students of a class. 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 lessons, we have 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++.

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.as:

struct address{
char streetAddress[100];
char city[50];
char country[50];

Now the structure address can be a part of student structure. We can rewrite student structure as under:

struct student{
char name[60];
address stdAdd;
float GPA;

Here stdAdd is a variable of type Address and a part of student structure. So we can have pointers and other structures in a structure. We can also have pointers to a structure in a structure. We know that pointer hold the memory address of the variable. If we have a pointer to an array, it will contain the memory address of the first element of the array. Similarly, the pointer to the structure points to the starting point where the data of the structure is stored.

The pointers to structure can be defined in the following manner i.e.

student *sptr;

Here sptr is a pointer to a data type of structure student. Briefly speaking, we have defined a new data type. Using structures we can declare:

• Simple variables of new structure
• Pointers to structure
• Arrays of structure

There are also limitation with structures as we can not say std1 + std2; 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 = s2. The assignment works because the structure is identical. So the name will be copied to the name, address to address and so on.

NEXT>>>>>Lesson 38. Working with Structures

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.