Articles by "Pointers"
Showing posts with label Pointers. Show all posts
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Short answer type Questions on Pointer

1. What is a pointer?
A pointer is a dynamic variable that stores address of a data value in a program.

2. How is a pointer declared in a C program?
syntax: datatype * pointer name;
Example: int *p, x=2;

3. Name the operators used in pointers?
* and & operators

4. How is a pointer initialized . give an ex.


Initializing a pointer means storing the address of the data variable in to a pointer.
Ex: p1=&x;

5. What is meant by a pointer to an array?
A pointer to an array stores address of the 1st data element of the array.
Ex: int *p, a[4];
p=a; 
or 
p=&a[0];

6. If ptr is a pointer to an integer what is the meaning of an expression ptr++; ?
ptr++; will increment the pointer and will make the pointer store address of next data element in the array.

7. What is meant by an array of pointers?
An array of pointer means a pointer array where every individual data is a pointer
variable that holds an address.
Ex: int  *p[10], a[10], b[10], c[10];
p[0]=&a[0];
p[1]=&b[0];
p[2]=&c[0];

8. Is it possible to add two pointers?
no.


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Wild Pointer in C++ language

Wild pointer are pointers that have not been initialized and can make a program crash or behave oddly. This is due to the fact that in the C++ programming language, pointers that are not specifically initialized point to unpredictable address in memory.

Write a C++ program in  which use of wild pointer are demonstrated.

   

#include<iostream>
using namespace std;
int main()
{
int *ptr ;
cout<<"Address on ptr is : "<< ptr;
cout<<endl;
cout<<"Value of address that is on ptr is : "<< *ptr;
return 0;
}


Explanation
: Here ptr is a wild pointer because it has not been initialized. There is a difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn't point any specific memory location.

The above program output is tested on www.jdoodle.com

Output:
Address on ptr is : 0x563a1ceceadc
Value of address that is on ptr is : -443987883


Thanks
Mukesh Rajput


no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
NULL Pointer in C++ language

NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.
For Example
int *ptr;
ptr = 0;
The above statement denotes ptr as an integer pointer type that does not point to a valid memory address. This shows that ptr has a NULL  pointer value.


The difference between void pointer and NULL pointers:

A void pointer is a special type of pointer of void type and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.  

Write a C++ program which demonstrate the use of NULL pointer.

#include<iostream>
using namespace std;
int *ptr = NULL;
int main()
{
*ptr  = 500;
cout<<"Value of ptr is : "<<*ptr;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:
Segmentation fault (core dumped)

2. Write a C++ program which demonstrate the use of NULL pointer.


#include<iostream>
using namespace std;
int *ptr = NULL;
int main()
{
int x=10;
ptr  = &x;
cout<<"Value of ptr is : "<<*ptr;
*ptr = 20;
cout<<"Value of ptr is : "<<*ptr;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:
Value of ptr is : 10
Value of ptr is : 20


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Void Pointers or Generic Pointers

The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type. Literal meaning of generic pointer is a pointer which can point any type of data. A void pointer is declared like a normal pointer, using the void keyword as the pointer's type:
void *ptr;
Here ptr is generic pointer. There are some points which should be kept in mind while deal with generic pointer and these are list below:

1. Generic Pointers cannot be dereference

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

The above program output is tested on www.jdoodle.com

Output:

jdoodle.cpp: In function 'int main()':
jdoodle.cpp:8:9: error: 'void*' is not a pointer-to-object type
 cout<< *ptr;
         ^
          or 
compile time error


2. sizeof() operator can be used to check size of Generic Pointers 

#include<iostream>
using namespace std;
int main()
{
int x=10;
void *ptr;
ptr = &x;
cout<< sizeof(ptr);
return 0;
}

The above program output is tested on www.jdoodle.com

Output:  8

3. Generic pointer can hold any type of pointers like char, struct pointer etc without ant typecasting

#include<iostream>
using namespace std;
int main()
{
char ch = 'x';
int i = 2;
void *ptr1;
char *ptr2 = &ch;
int *ptr3 = &i;
ptr1 = ptr2;
cout<< *(char *)ptr2;
cout<<endl;
ptr1 = ptr3;
cout<<*(int *)ptr1;
cout<<endl;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:  
2


Further in Generic pointer are used when we want to return such pointer which is applicable to all types of pointers.

Thanks 
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Use of Bit-wise Arithmetic operators with Pointers

We can perform Bit-wise operation between two pointers like:
Address1 & Address2 = Illegal
Address1 | Address2 = Illegal
Address1 ^ Address2 = Illegal
~Address = Illegal


Lets explain Bit-wise operator illegal use with the help of a C++ program

a. Write a C++ program which demonstrate the use of Bit-wise OR (|) use with pointers.  

#include<iostream>
using namespace std;
int main()
{
int x=10;
int y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1|ptr2;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:

jdoodle.cpp: In function 'int main()':
jdoodle.cpp:9:70: error: no match for 'operator|' (operand types are 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' and 'int*')
 cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1|ptr2;

or
compile time error

b. Write a C++ program which demonstrate the use of Bit-wise AND (&) use with pointers.  

#include<iostream>
using namespace std;
int main()
{
int x=10;
int y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1 & ptr2;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:

jdoodle.cpp: In function 'int main()':
jdoodle.cpp:9:71: error: no match for 'operator&' (operand types are 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' and 'int*')
 cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1 & ptr2;


or
compile time error


Thanks
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Illegal Arithmetic operation on Pointer in C++ language

In pointer arithmetic only pointer subtraction is possible and other operation link addition, multiplication, divide and modulus are not possible. i.e 
Address1  +  Address2 = Illegal
Address1  *  Address2 = Illegal
Address1  /  Address2 = Illegal
Address1 % Address2 = Illegal
Address1 - Address1 = (simple subtraction of two address) / Size of datatype which pointer points.

Write a program which demonstrate the arithmetic operation on pointer.

a. program which demonstrate the subtraction operation on pointer

#include<iostream>
using namespace std;
int main()
{
int x=10, y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<"Difference between ptr2 and ptr1 is :"<< ptr2-ptr1;
return 0;
}

The above program output is tested on www.jdoodle.com

Output:
Address of ptr1 is :0x7ffd55431770
Address of ptr2 is :0x7ffd55431774
Difference between ptr2 and ptr1 is :1

b. program which demonstrate the addition operation on pointer

#include<iostream>
using namespace std;
int main()
{
int x=10, y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<"Difference between ptr2 and ptr1 is :"<< ptr2+ptr1;
return 0;
}

The program output is tested on www.jdoodle.com
Output:

jdoodle.cpp: In function 'int main()':
jdoodle.cpp:12:54: error: invalid operands of types 'int*' and 'int*' to binary 'operator+'
 cout<<"Difference between ptr2 and ptr1 is :"<< ptr2+ptr1;

c. program which demonstrate the multiplication operation on pointer


#include<iostream>
using namespace std;
int main()
{
int x=10, y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<"Difference between ptr2 and ptr1 is :"<< ptr2*ptr1;
return 0;
}

The program output is tested on www.jdoodle.com
Output:

jdoodle.cpp: In function 'int main()':
jdoodle.cpp:12:54: error: invalid operands of types 'int*' and 'int*' to binary 'operator*'
 cout<<"Difference between ptr2 and ptr1 is :"<< ptr2*ptr1;

Thanks
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Some C++ program which perform the pointer operations in C++ language:

Note: If you subtract two pointers result will be a number but number will not be a simple mathematical subtraction of two addresses but it follow some rules:

If two pointers are of same type then:


Address2 - Address1 = (simple subtraction of two address) / Size of datatype which pointer points.

1. Write a program which perform the difference operation on pointer variables.

#include<iostream>
using namespace std;
int main()
{
int x=10;
int *ptr = &x;
int *temp;
temp = ptr;
// Here we increment the pointer with 1, means pointer is incremented with memory size 8 byte (4 for each int)
ptr = ptr + 2;
cout<<" Address of temp is : "<< temp;
cout<<endl;
cout<<" Address of pointer is : "<< ptr;
cout<<endl;
cout<<" Difference : "<< ptr - temp;
cout<<endl;
return 0;
}

Explanation: Here two pointer ptr and temp are of same type and both are pointing to int datatype variable.
ptr-temp = (0x7fff6ecf0bcc - 0x7fff6ecf0bc4 ) sizeof(int)
                  = 8/4 
                  = 2.

The output of this program is tested on www.jdoodle.com


Output:

Address of temp is : 0x7fff6ecf0bc4
Address of pointer is : 0x7fff6ecf0bcc

Difference between ptr and temp : 2

Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Arithmetic Operation with Pointer in C++ language

The different arithmetic operations which can be performed with pointers are given below:

1. Addition operation with pointers in C++:

If we will add or subtract a number from an address the result will also be an address. For example, Address + number = New_ Address  
Address - number = New_Address 
Address++ = New_Address 
Address-- = New_Address
++Address = New_Address 
--Address = New_Address. 

Here the New_Address = Address + number * Size of Datatype which pointer is pointing

Programming Example related to Pointer addition:

1. Write a C++ program which operate Arithmetic operators with pointer.

#include<iostream>
using namespace std;
int main()
{
int x=10;
int *ptr = &x;
cout<<"First time the address of pointer is : "<< ptr;
cout<<endl;
// Here we increment the pointer with 1, means poinetr is incremented with memory size 4 byte (for int)

ptr = ptr + 1; 
cout<<" Address of pointer after one integer increment : "<< ptr;
cout<<endl;
// Here again we increment the pointer with 1, means poinetr is incremented with memory size 4 byte (for int)
ptr = ptr + 1;
cout<<" Address of pointer after one integer increment again : "<<ptr;
return 0;
}

The output of this program is tested on www.jdoodle.com

Note: The output of the above program which is given below varies computer to computer, because every computer has their own memory location. 

Output:
First time the address of pointer is : 0x7ffe2ad12fdc
Address of pointer after one integer increment : 0x7ffe2ad12fe0
Address of pointer after one integer increment again : 0x7ffe2ad12fe4


2. Addition operation on pointer in C++:

#include<iostream>
using namespace std;
int main()
{
int x=10;
int *ptr = &x;
int *temp;
temp = ptr;
// Here we increment the pointer with 1, means poinetr is incremented with memory size 4 byte (for int)
ptr = ptr + 1;
cout<<" Address of pointer is : "<< ptr;
cout<<endl;
cout<<" Address stored in temp pointer is : "<< &temp;
cout<<endl;
return 0;
}

Note: In the above program the value stored in the temp varies as the address of the ptr varies. Here, line code " temp = ptr;" store the ptr value in temp. In the next line " ptr = ptr + 1;" change the value of ptr to one ( int). The change in ptr automatically change the value of temp also. The output of the above program which is given below varies computer to computer, because every computer has their own memory location.

The output of this program is tested on www.jdoodle.com


Output:
Address of pointer is : 0x7fff0a016c58
Address stored in temp pointer is : 0x7fff0a016c58


Thanks
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Pointer is C++ Language

Pointer is most powerful tool of C++. It is a variable to store the address of variable. Every variable stored in the unique memory location in computer. The pointer is the different beast because it hold the memory addresses as its value and has an ability to point to certain value within a memory location. A pointer is one of the most important concepts in C and C++ language. Pointer is a variable that stores a memory address. 


Use of Pointer Operators ( & and *):

There are two main operators which we can use as pointers operators:
1. & : Address of the variable operator
2. * : value at that address

Lets explain all these pointer operators with the help of a C++ program:

#include<iostream>
using namespace std;
int main()
{
int x = 20;
int *y; // pointer declaration 
y = &x; // pointer variable y assigned with address of x as &x contain address of x 
cout<<"Value of y is : "<< y<<endl;;
cout<<"Value of *y is : "<< *y<<endl;
cout<<"Value of &y is : "<< &y<<endl;
cout<<"Value of x is : "<< y<<endl;
cout<<"Value of &x is : "<< &x<<endl;
return 0;
}

This program output is tested on www.jdoodle.com

Output:
Value of y is : 0x7ffdddc195fc       // address of x

Value of *y is : 20                         // Value of *y is equal to x
Value of &y is : 0x7ffdddc19600 // Address of y
Value of x is : 20                          // value of x
Value of &x is : 0x7ffdddc195fc // Address of x

Thanks
Mukesh Rajput