Pointers in C++ Notes | Pointer Declaration, Pointer Arithmetic & Applications | Basic Computer Engineering | RGPV BTech First Year

Pointers in C++ Notes | Pointer Declaration, Pointer Arithmetic & Applications | Basic Computer Engineering | RGPV BTech First Year


Pointers in C++

Pointer C++ Programming का सबसे महत्वपूर्ण एवं Powerful Concept है। Pointer एक विशेष प्रकार का Variable होता है जो किसी अन्य Variable की Value Store नहीं करता, बल्कि उस Variable का Memory Address Store करता है। इसी कारण Pointer की सहायता से Memory को Direct Access किया जा सकता है तथा बड़े Programs अधिक Efficient बनाए जा सकते हैं।

C++ में Pointer का उपयोग Dynamic Memory Management, Arrays, Functions, Data Structures, Linked List, Trees, Graphs तथा Operating System जैसे Advanced Concepts में किया जाता है। Pointer को समझे बिना C++ Programming की गहराई को समझना कठिन माना जाता है।

RGPV B.Tech First Year के Basic Computer Engineering Subject में Pointers in C++ अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Pointer Declaration, Address Operator, Indirection Operator तथा Pointer Arithmetic से नियमित रूप से प्रश्न पूछे जाते हैं।

Variable



Memory Address



Pointer



Access Data

Introduction to Pointers

जब किसी Variable को Declare किया जाता है, तब Compiler उसके लिए Memory Allocate करता है। प्रत्येक Variable का एक Unique Memory Address होता है। सामान्य Variables केवल Data Store करते हैं, जबकि Pointer Variables उस Data का Address Store करते हैं।

Pointer के माध्यम से किसी Variable की Memory Location तक सीधे पहुँचा जा सकता है। यही कारण है कि Pointer को Memory Oriented Programming का आधार माना जाता है।


What is a Pointer?

Pointer ऐसा Variable है जो किसी अन्य Variable का Memory Address Store करता है। Pointer की सहायता से उस Variable की Value को अप्रत्यक्ष रूप (Indirectly) से Access तथा Modify किया जा सकता है।

int a = 10; int *ptr = &a;

ऊपर दिए गए उदाहरण में ptr एक Integer Pointer है जो Variable a का Address Store करता है।


Need of Pointers

Pointers का उपयोग केवल Memory Address Store करने के लिए ही नहीं बल्कि Efficient Programming तथा Advanced Data Structures बनाने के लिए भी किया जाता है।

  • Memory को Direct Access करने के लिए।
  • Dynamic Memory Allocation के लिए।
  • Arrays एवं Strings को Efficient तरीके से Handle करने के लिए।
  • Functions में Large Data Efficiently Pass करने के लिए।
  • Linked List, Stack, Queue, Tree एवं Graph जैसे Data Structures बनाने के लिए।
  • Execution Speed बढ़ाने के लिए।
  • Memory Utilization को बेहतर बनाने के लिए।

Advantages of Pointers

Advantage Description
Direct Memory Access Memory Location को सीधे Access किया जा सकता है।
Dynamic Memory Runtime पर Memory Allocate की जा सकती है।
Fast Execution Large Data के साथ Efficient Processing होती है।
Supports Data Structures Linked List, Tree एवं Graph बनाने में उपयोगी।
Memory Efficient अनावश्यक Data Copying से बचाता है।

Characteristics of Pointers

Pointers की कुछ महत्वपूर्ण विशेषताएँ (Characteristics) होती हैं जो इन्हें सामान्य Variables से अलग बनाती हैं। Pointer किसी Variable का Address Store करता है तथा उसी Address के माध्यम से Data को Access एवं Modify किया जा सकता है।

  • Pointer हमेशा Memory Address Store करता है।
  • प्रत्येक Pointer का अपना Data Type होता है।
  • Pointer की सहायता से Indirect Data Access किया जाता है।
  • Pointer Arithmetic संभव है।
  • Pointers Dynamic Memory Allocation को Support करते हैं।
  • Pointers Functions एवं Arrays के साथ Efficient तरीके से कार्य करते हैं।
  • Advanced Data Structures Pointer पर आधारित होती हैं।

Memory Address Concept

जब किसी Variable को Declare किया जाता है, तब Compiler उसके लिए Memory Allocate करता है। प्रत्येक Variable का एक Unique Memory Address होता है। यही Address Pointer Variable में Store किया जाता है।

Variable Value Memory Address (Example)
a 10 1000
b 25 1004
c 50 1008

ऊपर दिए गए Memory Addresses केवल उदाहरण (Illustration) के लिए हैं। वास्तविक Memory Address प्रत्येक Computer System में अलग हो सकते हैं।


Pointer Declaration

Pointer Declare करने के लिए Data Type के बाद * (Asterisk Operator) लगाया जाता है। Pointer का Data Type उसी Variable के Data Type के समान होना चाहिए जिसका Address उसमें Store किया जाएगा।

Syntax

data_type *pointer_name;

Examples

int *ptr; float *salary; char *ch; double *num;

ऊपर दिए गए उदाहरणों में प्रत्येक Pointer केवल उसी Data Type के Variable का Address Store कर सकता है।


Pointer Initialization

Pointer Declare करने के बाद उसमें किसी Variable का Address Store किया जाता है। Address Store करने के लिए Address Operator (&) का उपयोग किया जाता है।

int number = 100; int *ptr = &number;

अब Pointer ptr Variable number का Address Store करता है तथा उसी Address के माध्यम से उसकी Value Access की जा सकती है।


Relationship between Variable and Pointer

Variable
number = 100



Memory Address
1000



Pointer
ptr = 1000

Pointer स्वयं Data Store नहीं करता बल्कि Variable का Memory Address Store करता है। इसी कारण Pointer के माध्यम से Original Variable को Access किया जा सकता है।


Address Operator (&)

Address Operator (&) का उपयोग किसी Variable का Memory Address प्राप्त करने के लिए किया जाता है। जब किसी Variable के पहले & लगाया जाता है, तब उसका Address प्राप्त होता है।

Syntax

&variable_name

Example

#include<iostream> using namespace std; int main() { int number = 50; cout << &number; return 0; }

इस Program में &number Variable number का Memory Address प्रदर्शित करेगा।


Indirection Operator (*)

Indirection Operator (*) अथवा Dereference Operator का उपयोग Pointer द्वारा Stored Memory Address पर उपलब्ध वास्तविक Value को Access करने के लिए किया जाता है।

Example

#include<iostream> using namespace std; int main() { int number = 100; int *ptr = &number; cout << *ptr; return 0; }

Output :

100

यहाँ *ptr Pointer में Stored Address पर उपलब्ध वास्तविक Value को Access करता है।


Working of Pointer

Variable
number = 100



Address
1000



Pointer
ptr = &number



*ptr = 100

Pointer पहले Variable का Address Store करता है और बाद में उसी Address के माध्यम से Variable की Value को Access करता है। यही Pointer का मुख्य कार्य है।


Pointer Representation in Memory

Variable Value Address
number 100 1000
ptr 1000 2000

ऊपर दिए गए उदाहरण में ptr स्वयं भी Memory में Store होता है, लेकिन उसकी Value number का Address होती है।


Basic Program using Pointer

#include<iostream> using namespace std; int main() { int a = 25; int *ptr = &a; cout << "Value = " << a << endl; cout << "Address = " << &a << endl; cout << "Pointer = " << ptr << endl; cout << "Dereference = " << *ptr; return 0; }

Sample Output

Value = 25 Address = 0x61ff08 Pointer = 0x61ff08 Dereference = 25

Memory Address प्रत्येक Computer तथा प्रत्येक Execution में अलग-अलग हो सकता है। इसलिए Address का Exact Output अलग होना सामान्य बात है।


NULL Pointer

NULL Pointer ऐसा Pointer होता है जो किसी भी Valid Memory Location की ओर Point नहीं करता। Pointer Declare करने के बाद यदि उसे किसी Variable का Address Assign नहीं किया गया है, तो उसे NULL से Initialize करना सुरक्षित माना जाता है। इससे Program में Invalid Memory Access से बचा जा सकता है।

Syntax

int *ptr = NULL;

Example

#include<iostream> using namespace std; int main() { int *ptr = NULL; cout << ptr; return 0; }

NULL Pointer किसी भी Variable का Address Store नहीं करता। आधुनिक C++ में nullptr का उपयोग करना अधिक उपयुक्त माना जाता है।


Void Pointer

Void Pointer ऐसा Generic Pointer होता है जो किसी भी Data Type का Address Store कर सकता है। इसे Generic Pointer भी कहा जाता है। Void Pointer का उपयोग करने से पहले उचित Data Type में Type Casting करना आवश्यक होता है।

int number = 50; void *ptr = &number;

Void Pointer स्वयं Data Type नहीं जानता, इसलिए Dereference करने से पहले Casting आवश्यक होती है।


Wild Pointer

Wild Pointer ऐसा Pointer होता है जिसे Declare तो किया गया हो लेकिन Initialize नहीं किया गया हो। ऐसा Pointer किसी अनिश्चित Memory Address की ओर संकेत करता है और इसका उपयोग करना Program के लिए खतरनाक हो सकता है।

int *ptr;

Wild Pointer का उपयोग करने से Program Crash या Undefined Behaviour हो सकता है। इसलिए Pointer को Declare करते समय उसे NULL अथवा किसी Valid Address से Initialize करना चाहिए।


Pointer Arithmetic

Pointer Arithmetic का अर्थ Pointer की सहायता से Memory Address पर Arithmetic Operations करना है। Pointer में Increment (++) तथा Decrement (--) करने पर वह अगले अथवा पिछले Memory Location की ओर Move करता है।

Example

int arr[5] = {10,20,30,40,50}; int *ptr = arr; ptr++;

यदि Pointer Integer Type का है, तो ptr++ करने पर Pointer अगले Integer Element के Address पर पहुँच जाता है।


Pointer Arithmetic Operations

Operation Meaning
ptr++ अगले Memory Location पर जाना
ptr-- पिछले Memory Location पर जाना
ptr + n n Elements आगे जाना
ptr - n n Elements पीछे जाना
ptr1 - ptr2 दो Pointers के बीच Distance ज्ञात करना

Program to Demonstrate Pointer Arithmetic

#include<iostream> using namespace std; int main() { int arr[3] = {10,20,30}; int *ptr = arr; cout << *ptr << endl; ptr++; cout << *ptr << endl; ptr++; cout << *ptr; return 0; }

Output

10 20 30

Advantages of Pointer Arithmetic

  • Arrays को Efficient तरीके से Traverse किया जा सकता है।
  • Execution Speed बढ़ती है।
  • Memory Access अधिक तेज़ होता है।
  • Dynamic Data Structures में उपयोगी है।
  • Large Data Processing में Performance बेहतर होती है।

Pointer and Arrays

C++ में Array तथा Pointer का आपस में गहरा संबंध होता है। Array का नाम (Array Name) स्वयं उसके पहले Element का Address दर्शाता है। इसलिए किसी Array का Base Address सीधे Pointer में Store किया जा सकता है।

int arr[5]={10,20,30,40,50}; int *ptr = arr;

ऊपर दिए गए उदाहरण में arr पहले Element का Address Return करता है तथा Pointer ptr उसी Address को Store करता है।


Accessing Array using Pointer

Pointer की सहायता से Array के प्रत्येक Element को Access किया जा सकता है। Pointer Arithmetic का उपयोग करके Array Traversing अधिक Efficient हो जाती है।

#include<iostream> using namespace std; int main() { int arr[5]={10,20,30,40,50}; int *ptr=arr; for(int i=0;i<5;i++) { cout<<*(ptr+i)<<" "; } return 0; }

Output

10 20 30 40 50

Array of Pointers

जब किसी Array के प्रत्येक Element में Pointer Store किया जाता है, तब उसे Array of Pointers कहा जाता है। प्रत्येक Element किसी अलग Variable अथवा Memory Location का Address Store कर सकता है।

int a=10,b=20,c=30; int *ptr[3]; ptr[0]=&a; ptr[1]=&b; ptr[2]=&c;

Pointer to Pointer

Pointer to Pointer ऐसा Pointer होता है जो किसी अन्य Pointer का Address Store करता है। इसे Double Pointer भी कहा जाता है।

int number=100; int *ptr=&number; int **pptr=&ptr;
Expression Meaning
number Actual Value
ptr Address of number
*ptr Value of number
pptr Address of ptr
**pptr Value of number

Passing Pointer to Function

Pointer को Function में Parameter के रूप में Pass किया जा सकता है। इससे Function Original Variable पर सीधे कार्य कर सकता है तथा बड़ी मात्रा में Data Copy करने की आवश्यकता नहीं रहती।

#include<iostream> using namespace std; void square(int *n) { *n=(*n)*(*n); } int main() { int a=5; square(&a); cout<<a; return 0; }

Output

25

Dynamic Memory Allocation (Introduction)

C++ में Runtime के दौरान Memory Allocate करने की प्रक्रिया को Dynamic Memory Allocation कहा जाता है। इसके लिए new Operator का उपयोग Memory Allocate करने तथा delete Operator का उपयोग Memory Release करने के लिए किया जाता है।

int *ptr = new int; *ptr = 500; delete ptr;

Dynamic Memory Allocation का उपयोग तब किया जाता है जब Program Execute होने से पहले Memory Size ज्ञात न हो।


Difference between Normal Variable and Pointer

Normal Variable Pointer
Actual Data Store करता है। Memory Address Store करता है।
Value Direct Access होती है। Value Indirectly Access होती है।
Address Operator द्वारा Address प्राप्त होता है। Dereference Operator द्वारा Value प्राप्त होती है।
Memory Access सामान्य होता है। Memory Access अधिक Efficient होता है।

Applications of Pointers

Pointers का उपयोग C++ Programming के लगभग सभी Advanced Areas में किया जाता है। Dynamic Memory Allocation, Data Structures, File Handling, Operating Systems तथा System Programming में Pointers की महत्वपूर्ण भूमिका होती है।

Application Purpose
Dynamic Memory Allocation Runtime पर Memory Allocate एवं Release करना।
Linked List Nodes को आपस में Connect करना।
Tree Hierarchical Data Structure बनाना।
Graph Complex Relationships को Store करना।
Function Parameter Passing Large Data को Efficient तरीके से Pass करना।
Operating System Memory एवं Resource Management।
Compiler Design Symbol Table तथा Memory Handling।
File Handling Memory Buffer को Manage करना।

Advantages of Pointers

  • Memory का Efficient उपयोग होता है।
  • Dynamic Memory Allocation संभव होती है।
  • Large Data को बिना Copy किए Functions में Pass किया जा सकता है।
  • Execution Speed बेहतर होती है।
  • Complex Data Structures आसानी से बनाए जा सकते हैं।
  • Arrays तथा Strings पर Fast Processing होती है।
  • Operating System एवं System Software Development में उपयोगी हैं।
  • Runtime Memory Management संभव बनाते हैं।

Limitations of Pointers

  • गलत Address Access करने पर Program Crash हो सकता है।
  • Wild Pointer एवं Dangling Pointer जैसी समस्याएँ उत्पन्न हो सकती हैं।
  • Debugging अपेक्षाकृत कठिन होती है।
  • Memory Leak की संभावना रहती है यदि Allocated Memory Release न की जाए।
  • गलत Pointer Arithmetic से Undefined Behaviour हो सकता है।

Difference between NULL Pointer and Wild Pointer

NULL Pointer Wild Pointer
NULL से Initialize किया जाता है। Initialize नहीं किया जाता।
Safe माना जाता है। Unsafe होता है।
किसी Valid Address को Point नहीं करता। Unknown Memory Address को Point कर सकता है।
Program Crash की संभावना कम होती है। Undefined Behaviour उत्पन्न हो सकता है।

Summary

Topic Key Point
Pointer Memory Address Store करता है।
Address Operator (&) Variable का Address प्राप्त करता है।
Dereference Operator (*) Pointer से Value Access करता है।
NULL Pointer Safe Initialization के लिए।
Void Pointer Generic Pointer।
Pointer Arithmetic Memory Locations पर Arithmetic Operations।
Pointer to Pointer Pointer का Address Store करता है।
Dynamic Memory new एवं delete Operator का उपयोग।

Important Viva Questions

  1. Pointer क्या है?
  2. Address Operator एवं Dereference Operator में क्या अंतर है?
  3. Pointer Declaration कैसे करते हैं?
  4. NULL Pointer क्या होता है?
  5. Void Pointer क्या है?
  6. Wild Pointer क्या है?
  7. Pointer Arithmetic क्या है?
  8. Pointer to Pointer क्या होता है?
  9. Array एवं Pointer में क्या संबंध है?
  10. Dynamic Memory Allocation क्या है?

University Examination Important Questions

  1. Pointer की परिभाषा लिखिए तथा इसकी आवश्यकता समझाइए।
  2. Pointer Declaration एवं Initialization उदाहरण सहित समझाइए।
  3. Address Operator तथा Dereference Operator को उदाहरण सहित समझाइए।
  4. Pointer Arithmetic क्या है? उदाहरण सहित समझाइए।
  5. NULL Pointer, Void Pointer तथा Wild Pointer में अंतर लिखिए।
  6. Pointer एवं Array के संबंध को उदाहरण सहित समझाइए।
  7. Pointer to Pointer क्या है?
  8. Passing Pointer to Function को उदाहरण सहित समझाइए।
  9. Dynamic Memory Allocation पर संक्षिप्त टिप्पणी लिखिए।
  10. Pointers के Applications एवं Advantages लिखिए।

Conclusion

Pointers in C++ Programming का अत्यंत महत्वपूर्ण Concept है। Pointer की सहायता से Memory को Direct Access किया जा सकता है तथा Dynamic Memory Management, Arrays, Functions और Data Structures को अधिक Efficient बनाया जा सकता है। Pointer Declaration, Address Operator, Dereference Operator, Pointer Arithmetic तथा Dynamic Memory Allocation को अच्छी तरह समझना प्रत्येक C++ Programmer एवं RGPV B.Tech विद्यार्थी के लिए अत्यंत आवश्यक है।

Related Articles

Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year

Arrays ...

Read More →

Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year

Object ...

Read More →

Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year

Pointer...

Read More →

Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year

Dynamic...

Read More →

this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More →