Learning C/C++ Step-By-Step - Page 10
On this page
- 10. Step-by-Step C/C++ --- C Programming - Structure
- Structures
- 1. Introduction
- 2. Declaration of a structure
- 3. Defining a Structure Variable
- 4. Initializing a Structure variable
- 5. Direct assignment of structures
- 6. Calculation of structure size
- 7. Nested Structures
- 8. Array of Structures
- 9. Arrays with in Structures
- 10. Passing Structures to Functions
- 11. Returning Structures from functions
- 12. Pointer to Structure
- 13. Structures Containing Pointers
- 14. Self Referential Structures
10. Step-by-Step C/C++ --- C Programming - Structure
Structures
1. Introduction 2. Declaration of Structure 3. Defining a Structure Variable 4. Initializing a Structure Variable 5. Direct assignment of structures 6. Calculation of Structure size 7. Nested Structures 8. Array of Structures 9. Arrays within Structures 10. Passing Structures to Function 11. Returning Structures from Functions 12. Pointer To structure 13. Structure containing Pointers 14. Self Referential Structures |
1. Introduction
int a[4] = { 3, 4, 5, 6 }; /* Valid expression */ |
Why the last two expressions are invalid? An array can store values of same type. Must be the same type. Where as a structure can hold more than one type of data according to its definition.
• A group of one or more variables of different data types organized together under a single name is called a structure or • A collection of heterogeneous (dissimilar) types of data grouped together under a single name is called a structure or • A structure is a collection of simple variables. The variable in a structure can be of different types. The data items in a structure are called the members of the structures. |
2. Declaration of a structure
When a structure is defined the entire group is referenced through the structure name. The individual components present in the structure are called as the structure members and these can be accessed and processed separately.
Eg:
struct date |
struct student |
3. Defining a Structure Variable
Defining a structure variable is the same as that for defining a built-in data type such as int.
int a; /* valid */ |
4. Initializing a Structure variable
The members of the structure can be initialized like other variables. This can be done at the time of declaration or at the design time.
1. Initialization at Declaration: |
2. Initialization at Definition: |
ddate d; |
4. Initialization at run time: |
Eg:
/* Write a program to accept and print the details of an employee */ |
5. Direct assignment of structures
Direct assignment of more than one variable is made possible using structures.
struct emp a, b = {1001, "Vimal", 6700.00 }; |
|
Output: 1001 Vimal 6700.00 |
6. Calculation of structure size
Every data type in C/C++ has a specified size, i.e int has 2 bytes of size, float has 4 bytes of size and so on. Here is the way to find the size of a structure variable.
sizeof :- This function is used to find the size of a given variable.
printf("%d", sizeof(int)); /* 2 */ |
7. Nested Structures
Structure with in structures in known as nested structures. For accessing nested structure members we must apply the dot operator twice in calling structure members.
Eg:
/* program to demonstrate nested structure with employee structure */ |
8. Array of Structures
We can create an array of structures. The array will have individual structures as its elements.
/* Write a program to accept and print the details of an employee */ |
Nothing is new in the above program. Entire program is same as simple structured program except the marked data.
9. Arrays with in Structures
There may be a situation to utilize arrays with in structures. How to achieve arrays with in structures. Here is the approach with simple program.
/* Program to accept and print a student information */ |
10. Passing Structures to Functions
It is possible to send entire structures to functions as arguments in the function call. The structure variable is treated as any ordinary variable.
/* Program to pass a structure variable to function */ |
11. Returning Structures from functions
We can return structures from functions. Yes structures can be returned from functions just as variables of any other type.
/* Returning structure object from a function */ |
12. Pointer to Structure
Till now we have seen that the members of a structure can be of data types like int, char, float or even structure. C/C++ language also permits to declare a pointer variable as a member to a structure. Pointer variables can be used to store the address of a structure variable also. A pointer can be declared as if it points to a structure data type.
/* Program to demonstrate the process of Pointer to structure */ |
The marked data is essential to implement pointer to structure.
The following statement is optional, but better to utilize to organize better memory management.
emp = (struct employee * )malloc(sizeof(emp));
13. Structures Containing Pointers
A pointer variable can also be used as a member in the structure.
The following program contains pointer members contained by a pointer variable of structure.
/* program to demonstrate the use of structures containing Pointers */ |
|
output: |
14. Self Referential Structures
Structures can have members, which are of the type the same structure itself in which they are included. This is possible with pointers and the phenomenon is called as self-referential structures.
struct emp |
Self-referential structures can be used mainly in arranging data, sorting, searching elements, insertion, deletion of elements and so on.
This way of approach leads to Data structures (i.e., Linked Lists, Stacks, Queues, Trees and Graphs).