Pointers and file concept:
Pointer Overview:
The symbol of an address is represented by a pointer. In addition to creating and
modifying dynamic data structures, they allow programs to emulate call-by-reference.
One of the principal applications of pointers is iterating through the components of
arrays or other data structures. The pointer variable that refers to the same data type as
the variable you're dealing with has the address of that variable set to it (such as an int
or string).
Syntax
• datatype *var_name;
• int *ptr; // ptr can point to an address which holds int data
Pointers to object:
A pointer is a variable that stores the memory address of another variable (or object) as its value.
A pointer aims to point to a data type which may be int, character, double, etc.
Pointers to objects aim to make a pointer that can access the object, not the variables.
There are two approaches by which you can access an object. One is directly and the other is by
using a pointer to an object in C++.
• syntax:
classname*pointertoobject;
For storing the address of an object into a pointer use the following syntax:
pointertoobject=&objectname;
Example 1. In the below example, a simple class named My_Class is created. An object of the
class is defined as named object. Here a pointer is also defined named p. In the program given
below program, it is shown how can we access the object directly and how can we use the
pointer to access the object directly.
// Example using an object pointer.
#include <iostream>
using namespace std;
class My_Class {
int num;
,public:
void set_number(int value) {num = value;}
void show_number();
};
void My_Class::show_number()
{
cout << num << "\n";
}
int main()
{
My_Class object, *p; // an object is declared and a pointer to it
object.set_number(1); // object is accessed directly
object.show_number();
p = &object; // the address of the object is assigned to p
p->show_number(); // object is accessed using the pointer
return 0;
}
Output:
1
1
// Increment and decrement are done of an object pointer.
#include <iostream>
using namespace std;
class My_Class {
int num;
public:
void set_number(int val) {num = val;}
void show_number();
};
void My_Class::show_number()
{
cout << num << "\n";
}
int main()
{
My_Class object[2], *p;
object[0].set_number(10); // objects is accessed directly
object[1].set_number(20);
p = &object[0]; // the pointer is obtained to the first element
p->show_number(); // value of object[0] is shown using pointer
p++; // advance to the next object
p->show_number(); // show value of object[1] is shown using the pointer
, p--; // retreat to previous object
p->show_number(); // again value of object[0] is shown
return 0;
}
Output:
10
20
10
Example 2. In the below example, we used a pointer to an object and an arrow operator.
#include<iostream>
using namespace std;
class Complex{
int real, imaginary;
public:
void get_Data(){
cout<<"Here the real part is "<< real<<endl;
cout<<"Here the imaginary part is "<< imaginary<<endl;
}
void set_Data(int x, int y){
real = x;
imaginary = y;
}
};
int main(){
Complex *ptr = new Complex;
ptr->set_Data(1, 54);
ptr->get_Data();
// Array of Objects
Complex *ptr1 = new Complex[4];
ptr1->set_Data(1, 4);
ptr1->get_Data();
return 0;
}
Output:
Here the real part is 1
Here the imaginary part is 54
Here the real part is 1
Here the imaginary part is 4
This pointer: