Introduction CPP Data Structures

Unveiling the Harmony of C/C++ Arrays and Structures

In the realm of programming, C and C++ shine as l

anguages that offer a symphony of data manipulation capabilities. Arrays and structures emerge as key players, each with its unique melody to compose. Arrays harmonize identical data items, while structures serenade a blend of diverse data elements. Exploring C++ Data Structures involves studying various ways of organizing and storing data to optimize efficiency and accessibility in programming.

Embarking on a Journey with Structures

Imagine the scenario of a library. Books—each a unique entity—are graced with attributes like title, author, subject, and book ID. To capture this orchestration of information, structures arise as the perfect ensemble.

The Overture: Defining a Structure

To introduce a structure, the struct statement takes center stage. This statement crafts a new data type, a vessel that can hold multiple members. The structure tag, an optional flourish, introduces the structure. Each member definition takes on the form of a standard variable declaration. At the climax of the definition, before the final semicolon, one or more structure variables can be introduced, although this is optional.

Here’s a glimpse of the notation:

struct [structure tag] {
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];

Unveiling the Melodies: Accessing Structure Members

A composition’s essence lies in its melodies, and accessing structure members is no different. The member access operator (.) creates a harmonic link between the structure variable and the desired member. The struct keyword comes into play when declaring structure variables.

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

Crafting a Sonata: A Full Example

Let’s compose a full piece using these elements:

#include <iostream>
#include <cstring>

using namespace std;

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main() {
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book

   // Book 1 specifications
   strcpy(Book1.title, "Learn C++ Programming");
   strcpy(Book1.author, "Chand Miyan"); 
   strcpy(Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book 2 specifications
   strcpy(Book2.title, "Telecom Billing");
   strcpy(Book2.author, "Yakit Singha");
   strcpy(Book2.subject, "Telecom");
   Book2.book_id = 6495700;

   // Print Book1 info
   cout << "Book 1 title: " << Book1.title << endl;
   cout << "Book 1 author: " << Book1.author << endl;
   cout << "Book 1 subject: " << Book1.subject << endl;
   cout << "Book 1 ID: " << Book1.book_id << endl;

   // Print Book2 info
   cout << "Book 2 title: " << Book2.title << endl;
   cout << "Book 2 author: " << Book2.author << endl;
   cout << "Book 2 subject: " << Book2.subject << endl;
   cout << "Book 2 ID: " << Book2.book_id << endl;

   return 0;
}

Concluding the Composition: Structures in Functionality

Structures are not mere notes; they form the foundation of functional compositions. Passing a structure to a function is as seamless as any other variable or pointer. Accessing structure variables within the function mirrors previous practices.

void printBook(struct Books book);

Elevating the Melody: Pointers to Structures

Structures take on a new dimension when introduced to pointers. Defining pointers to structures and accessing structure members using the -> operator allows a symphony of dynamic interactions.

struct Books *struct_pointer;
struct_pointer = &Book1;

The Encore: The Typedef Keyword

The curtain falls on this orchestral performance with the typedef keyword. It offers an elegant alias for types, introducing an encore of simplicity and elegance.

typedef struct {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Books;

With this finale, the opus of C/C++ arrays and structures resonates—a harmonious blend of melodies, each instrument contributing to the grand symphony of programming.


Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe now to keep reading and get access to the full archive.

Continue reading