C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Data Structures

C++ Data Structures

shape Introduction

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

shape 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
The tag name is optional and can be left out.

shape Syntax

struct structure_name { data_type member_name1; data_type member_name2; } object_names;

Declaration and Initialization of structure

shape Description

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:
struct student { char name[20]; int age; float percentage; } student1={"mike",15,77.5}; student2{"jack",16,78};

shape Example

[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

shape 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.

Pointers to Structures

shape 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.

shape 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

shape Description

Structs can be used over individual variables to pass the entire struct to a function that needs to work with the members.

shape Example

[c] #include <iostream> struct Emp { short id; int age; double wage; }; void printInformation(Emp emp) { std::cout << "ID: " << emp.id << "\n"; std::cout << "Age: " << emp.age << "\n"; std::cout << "Wage: " << emp.wage << "\n"; } int main() { Emp John = { 14, 32, 24.15 }; Emp Mike = { 15, 28, 18.27 }; // Print John's information printInformation(John); std::cout << "\n"; // Print Mike's information printInformation(Mike); return 0; } [/c] Output: [c] ID: 14 Age: 32 Wage: 24.15 ID: 15 Age: 28 Wage: 18.27 [/c]

C++ Unions

shape Description

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.

shape Conceptual figure

shape 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]

shape 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

shape 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.

shape 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.