In C++ programming, a variable consists of single data and single data type. An array consists of multiple data and single data type. Continuing in advance, CPP Data Structures consists of multiple data and multiple data types.
Structure
Description
Structure can be defined as group of variables with same name and different data types.
Every member of structure is called as structure "member" (or) an "element". Every member of structure are given different memory space and can be accessed individually.
A struct has three parts:
Keyword struct
Tag which is a optional identifier
Member Data Fields of the struct listed inside parentheses
Any structure is initialized in order to access it and allocate a memory to it.
Declaring will be same as defining. Coming to defining CPP Data Structures, assign values to variables at the end of structure block( after the braces) by giving a semicolon ;.
Eg:
[c]
#include<iostream>
using namespace std;
struct college
{
char *name;
int rno;
};
int main()
{
struct college c;
c.name = "splessons";
c.rno = 22;
cout << "Given name is: " << c.name << '\n';
cout << "Telephone number: " << c.rno;
return 0;
}[/c]
Output
[c]
Given name is: splessons
Telephone number: 22[/c]
Retrieving a structure
Description
CPP Data Structures can be accessed using the dot . operator. Structure can also be accessed by using ->. As using arrow is bit complex, dot operator is used.
Note:
Just declaring the structure will never allocate memory to it.Structure must be initialized.
Pointers to Structures
Description
Structures can be pointed to by its own type of pointers.Pointers in structures are used by placing -> instead of . and can be used with pointers to objects that have members. This operator serves to access the member of an object directly from its address.
Example
[c]
#include<iostream>
using namespace std;
typedef struct website
{
char *name;
int rank;
}college;
int main()
{
college index;
college *ptr_myweb;
ptr_myweb = &index;
ptr_myweb->name = "splessons";
ptr_myweb->rank = 1;
cout << "Name of website: " << ptr_myweb->name << '\n';
cout << "website rank: " << ptr_myweb->rank;
return 0;
}[/c]
Output
[c]
Name of website: splessons
website rank: 1[/c]
Structs and Functions
Description
Structs can be used over individual variables to pass the entire struct to a function that needs to work with the members.
Union is very similar to structures. The keyword used is "union". Union is used mainly to save the memory space and to store the values dynamically. The differences between union and structure is,
Structure allocates memory for every member of it separately. Whereas union allocates a single shared storage space for all of its members which will be the size of its biggest data member.
Manipulations made on one member will not effect the other in structures. As only single member is allocated at a time in unions, manipulations done on one member will reflect on other.
Conceptual
figure
Syntax
union union_name
{
//declarations and initializations of members;
};
Accessing the union member
The union variable can be retrieved in the same manner as in the structure. To access the union variable . dot operator is used and to get the address of union variable * is used.
Unions are not much used when compared to structures as they does not store all the variables at a time.
Declaring a union
[c]union example
{
char c;
int a;
float b;
} ex;[/c]
Initializing a union
[c]union example
{
char c;
int a;
float b;
} ex;
ex.c;
ex.a;
ex.b;[/c]
Example
[c]
#include<iostream>
using namespace std;
typedef union splessons
{
double pi_value;
int B;
}splessons;
int main()
{
splessons numbers;
numbers.pi_value = 3.14;
cout<< " Value of pi is : "<< numbers.pi_value<<endl;
return 0;
}[/c]
Output
[c]
Value of pi is : 3.14[/c]
Summary
Key Points
Variables of different data type and same name in structures can be accessed with dot operator.
Every structure member are given different memory space and can be accessed individually.
CPP Data Structures “struct” keyword is replaced with “union” and is used in shareable mode.
Unions are used to focus on memory as each member within union structure is assigned its own unique storage space.
Programming
Tips
When there are no values to declare, CPP Data Structures should be declared as null. Otherwise, there will be no memory allocation for the variable.
Do not initialize multiple members using union.The compiler gives an error.