Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 46: Pointers in C++ – Basics, Declaration, and Examples | Class 11

Learn about pointers in C++ with Day 46 of Class 11 Computer Science. Covers basics, declaration, operations, and practical examples with outputs.

Day 46 – Introduction to Pointers in C++

Class 11 Computer Science

What are Pointers in C++? (C++ में Pointers क्या हैं?)

A pointer is a variable in C++ that stores the memory address of another variable. It provides an efficient way to access and manipulate data stored in memory.
C++ में Pointer एक ऐसा वेरिएबल है जो किसी अन्य वेरिएबल के मेमोरी एड्रेस को स्टोर करता है। यह मेमोरी में संग्रहीत डेटा तक पहुंचने और उसे हेरफेर करने का कुशल तरीका प्रदान करता है।

Declaration of Pointers (Pointers की घोषणा)

To declare a pointer, use the * operator along with the data type of the variable it will point to.
Pointer को घोषित करने के लिए, * ऑपरेटर का उपयोग करें और उस वेरिएबल के डेटा प्रकार को निर्दिष्ट करें जिसे यह पॉइंट करेगा।

Syntax:

 *pointer_name;

Example:

int *ptr; // Pointer to an integer
float *fptr; // Pointer to a float

Pointer Operations (Pointer पर किए जाने वाले ऑपरेशन्स)

  • Address-of Operator (&): Retrieves the address of a variable.
    वेरिएबल का एड्रेस प्राप्त करता है।
  • Dereference Operator (*): Accesses the value stored at the memory address.
    मेमोरी एड्रेस पर संग्रहीत मान तक पहुंचता है।

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int *ptr = &x; // Pointer stores the address of x

    cout << "Address of x: " << ptr << endl;
    cout << "Value of x: " << *ptr << endl; // Dereferencing pointer

    return 0;
}

Output:

Address of x: 0x7ffee1a1b08c (Example output - actual address may vary)
Value of x: 10

Advantages of Pointers (Pointers के लाभ)

  • Allows dynamic memory allocation.
    डायनामिक मेमोरी अलोकेशन की अनुमति देता है।
  • Facilitates the creation of complex data structures like linked lists and trees.
    जटिल डेटा संरचनाओं जैसे लिंक्ड लिस्ट और ट्री बनाने की सुविधा प्रदान करता है।
  • Enables efficient passing of large data structures to functions.
    बड़े डेटा संरचनाओं को फ़ंक्शन में कुशलता से पास करने में सक्षम बनाता है।

Common Errors with Pointers (Pointers में सामान्य त्रुटियाँ)

  • Dereferencing a null pointer.
    Null Pointer का Dereference करना।
  • Dangling pointers that reference deallocated memory.
    Deallocated मेमोरी को संदर्भित करने वाले Dangling Pointers।
  • Memory leaks due to improper deallocation.
    अनुचित मेमोरी Deallocation के कारण मेमोरी लीक।

Examples of Pointers in C++ (C++ में Pointers के उदाहरण)

Example 1: Basic Pointer Operations

#include <iostream>
using namespace std;

int main() {
    int a = 20;
    int *p = &a;

    cout << "Address of a: " << p << endl;
    cout << "Value of a: " << *p << endl;

    return 0;
}

Output:

Address of a: 0x7ffee1a1b0c8 (Example output - actual address may vary)
Value of a: 20

Example 2: Pointer Arithmetic

#include <iostream>
using namespace std;

int main() {
    int arr[3] = {10, 20, 30};
    int *p = arr;

    cout << "First element: " << *p << endl;
    p++;
    cout << "Second element: " << *p << endl;
    p++;
    cout << "Third element: " << *p << endl;

    return 0;
}

Output:

First element: 10
Second element: 20
Third element: 30

Practice Questions

Multiple Choice Questions (MCQs)

  1. What does a pointer store?
    (a) Value of a variable | (b) Memory address | (c) Data type | (d) None
  2. Which operator is used to get the address of a variable?
    (a) & | (b) * | (c) + | (d) None
  3. Which operator is used to access the value at the memory address stored in a pointer?
    (a) * | (b) & | (c) ++ | (d) None
  4. What is the correct way to declare a pointer to an integer?
    (a) int p | (b) int *p | (c) int p* | (d) None
  5. What happens if you dereference a null pointer?
    (a) Value is retrieved | (b) Error | (c) Undefined behavior | (d) None
  6. What is a dangling pointer?
    (a) A pointer not initialized | (b) A pointer pointing to deallocated memory | (c) A pointer pointing to null | (d) None
  7. Which of the following is NOT an advantage of pointers?
    (a) Dynamic memory allocation | (b) Simplified debugging | (c) Data structure implementation | (d) None
  8. What is the size of a pointer variable?
    (a) Depends on the data type | (b) Depends on the system architecture | (c) Always 4 bytes | (d) None
  9. What does *ptr mean in C++?
    (a) Value at the memory address stored in ptr | (b) Address of ptr | (c) Pointer increment | (d) None
  10. What is the output of the following code?
    int x = 5;
    int *p = &x;
    cout << *p;
            
    (a) 5 | (b) Address of x | (c) Error | (d) None

Answers to MCQs:

1: (b), 2: (a), 3: (a), 4: (b), 5: (c), 6: (b), 7: (b), 8: (b), 9: (a), 10: (a)

Short Answer Questions

  1. What is a pointer in C++?
    Answer: A pointer is a variable that stores the memory address of another variable.
  2. Write the syntax to declare a pointer to an integer.
    Answer:
    int *ptr;
    
  3. What is the use of the & operator in pointers?
    Answer: The & operator retrieves the memory address of a variable.
  4. What are dangling pointers?
    Answer: Dangling pointers point to memory that has been deallocated.
  5. Write a program to demonstrate pointer arithmetic.
    Answer:
    int arr[3] = {10, 20, 30};
    int *p = arr;
    cout << *p << endl;
    p++;
    cout << *p << endl;
    

Long Answer Questions

  1. Explain pointers in C++ with examples of declaration and usage.
  2. Discuss the advantages and disadvantages of using pointers in programming.
  3. Write a program to find the sum of elements in an array using pointers.

Post a Comment