STRUCTURES AND POINTERS in C++

 STRUCTURES AND POINTERS

 Introduction to Structures

A structure is a user-defined data type that allows to combine data items of different types under one name.

It is used to represent a record (like a student, employee, book, etc.)


 Syntax

struct StructureName {

    dataType member1;

    dataType member2;

    ...

};


Example

#include <iostream>

using namespace std;

 

struct Student {

    int roll;

    string name;

    float marks;

};

 

int main() {

    Student s1;  // Structure variable declaration

    s1.roll = 101;

    s1.name = "Pankaj";

    s1.marks = 92.5;

 

    cout << "Roll: " << s1.roll << endl;

    cout << "Name: " << s1.name << endl;

    cout << "Marks: " << s1.marks << endl;

    return 0;

}


Output

Roll: 101

Name: Pankaj

Marks: 92.5


Accessing Structure Members

To access members of a structure, use the dot (.) operator with the structure variable name.

 

Example:
s1.roll, s1.name, s1.marks


 

Array of Structures

Create multiple records using an array of structures.

 

Example:-

#include <iostream>

using namespace std;

 

struct Student {

    int roll;

    string name;

    float marks;

};

 

int main() {

    Student s[3];  // Array of 3 Students

 

    for (int i = 0; i < 3; i++) {

        cout << "Enter Roll, Name, Marks: ";

        cin >> s[i].roll >> s[i].name >> s[i].marks;

    }

 

    cout << "\nStudent Details:\n";

    for (int i = 0; i < 3; i++) {

        cout << s[i].roll << " " << s[i].name << " " << s[i].marks << endl;

    }

 

    return 0;

}


 

Pointer to Structure

A pointer to a structure is a variable that stores the address of a structure variable.

 

Syntax

StructureName *ptr;

ptr = &structureVariable;

 

Access structure members in two ways:

  • Using dot operator (.) with normal variable
  • Using arrow operator (->) with pointer variable

Example

#include <iostream>

using namespace std;

 

struct Student {

    int roll;

    string name;

    float marks;

};

 

int main() {

    Student s1 = {101, "Pankaj", 92.5};

    Student *ptr = &s1;  // Pointer to structure

 

    cout << "Using pointer:\n";

    cout << "Roll: " << ptr->roll << endl;

    cout << "Name: " << ptr->name << endl;

    cout << "Marks: " << ptr->marks << endl;

 

    return 0;

}


Output

Using pointer:

Roll: 101

Name: Pankaj

Marks: 92.5


 

 Structure with Pointer Members

Use pointers as structure members.

 

Example:

#include <iostream>

using namespace std;

 

struct Book {

    string title;

    float *price; // pointer member

};

 

int main() {

    float p = 299.99;

    Book b1 = {"C++ Programming", &p};

 

    cout << "Book: " << b1.title << endl;

    cout << "Price: " << *(b1.price) << endl;

    return 0;

}


 

Dynamic Memory Allocation with Structures

Dynamically allocate memory for structures using new.

 

Example:

#include <iostream>

using namespace std;

 

struct Student {

    int roll;

    string name;

};

 

int main() {

    Student *ptr = new Student;  // Dynamic memory allocation

 

    ptr->roll = 102;

    ptr->name = "Rahul";

 

    cout << "Roll: " << ptr->roll << endl;

    cout << "Name: " << ptr->name << endl;

 

    delete ptr;  // Free memory

    return 0;

}


 

Passing Structure to Functions

Three ways to pass structure to a function:

Method

Description

Pass by Value

Copy of structure is passed

Pass by Reference

Function can modify original data

Pass by Pointer

Function receives address (efficient way)


 

Example: Pass by Pointer

#include <iostream>

using namespace std;

 

struct Student {

    int roll;

    string name;

};

 

void show(Student *ptr) {

    cout << "Roll: " << ptr->roll << endl;

    cout << "Name: " << ptr->name << endl;

}

 

int main() {

    Student s = {103, "Amit"};

    show(&s);   // Passing address of structure

    return 0;

}


 

Nested Structures

A structure inside another structure.

#include <iostream>

using namespace std;

 

struct Address {

    string city;

    int pincode;

};

 

struct Student {

    int roll;

    string name;

    Address addr;  // Nested structure

};

 

int main() {

    Student s1 = {104, "Neha", {"Jaipur", 302001}};

    cout << s1.name << " lives in " << s1.addr.city << " - " << s1.addr.pincode;

    return 0;

}


Summary 

Concept

Explanation

Example

Structure

User-defined datatype

struct Student {int roll;};

Dot Operator

Access structure members

s1.roll

Pointer to Structure

Stores the address of the structure

Student *ptr = &s1;

Arrow Operator

Access members using a pointer

ptr->name

Array of Structures

Store multiple records

Student s[5];

Dynamic Structure

Allocated using the new

Student *ptr = new Student;

Nested Structure

Structure inside another

struct Student { Address addr; };



 

Post a Comment

0 Comments