0% found this document useful (0 votes)
20 views13 pages

4 Pointers

The pointer is a variable that stores the memory address of another variable. Pointers allow access to memory locations and can be used to pass variables by reference. Pointers reduce code size and improve performance by retrieving strings, trees, and being used with arrays, structures, and functions.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

4 Pointers

The pointer is a variable that stores the memory address of another variable. Pointers allow access to memory locations and can be used to pass variables by reference. Pointers reduce code size and improve performance by retrieving strings, trees, and being used with arrays, structures, and functions.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Pointer

• The pointer is a variable which stores the address of another variable.


• This variable can be of type int, char, array, function, or any other pointer.
• A pointer can also be used to refer to another pointer function.
• The purpose of pointer is to save memory space and achieve faster execution
time.
• Syntax:
data_type * pointer_variable _name;
The asterisk (*) is being used to designate a variable as a pointer.
Example:
#include<iostream>
using namespace std;
int main()
{
int number = 50;
int *p; // declaration of pointer
p = &number; //stores the address of number variable in pointer p
// p contains the address of the number therefore printing p gives the address of number.
cout<<"Address of number variable is“<<p;
/
*As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address
contained by p. */
cout<<"Value of p variable is “<<*p; // print value of the number = 50
return 0;
}
Advantage of pointer

• Pointer reduces the code and improves the performance, it is used to


retrieving strings, trees, etc. and used with arrays, structures, and functions.
• We can return multiple values from a function using the pointer.
• It makes you able to access any memory location in the computer's memory.
Types of pointers
NULL pointer: It is always a good practice to assign a NULL value to a
pointer variable in case you do not have an exact address to be assigned. This
is done at the time of variable declaration. A pointer that is assigned NULL is
called a null pointer. #include <iostream>
Example: using namespace std;
int main ()
{
int *ptr = NULL;
cout<<"The value of ptr is :“<< ptr;
return 0;
Output: }
The value of ptr is 0
Void Pointer: A void pointer is also called as a generic pointer. It does not have any standard data type.
A void pointer is created by using the keyword void. It can be used to store an address of any variable.
Example:

#include <iostream>
using namespace std;
int main()
{
void *p = NULL; //void pointer
cout<<"The size of pointer is:\n“<<sizeof(p);
return 0;
}

Output:
The size of pointer is:4
• Wild pointer: A pointer is said to be a wild pointer if it is not being initialized to
anything. These types of C pointers are not efficient because they may point to
some unknown memory location which may cause problems in our program and
it may lead to crashing of the program.

• Dangling pointer: A pointer pointing to a memory location that has been deleted
(or freed) is called dangling pointer.
There are three different ways where Pointer acts as dangling pointer:
1) De-allocation of memory: deallocate the memory to which pointer is
pointing
2) Variable goes out of scope: data type of pointer and variable is different
3) Function call: pointer is pointing to local variable and if value of variable
changed after function call then the pointer will become dangling pointer
Pointers arithmetic

• There are only a few operations that are allowed to perform on pointers,
these are:
1.Increment/Decrement of a Pointer
2.Addition of integer to a pointer
3.Subtraction of integer to a pointer
4.Subtracting two pointers of the same type
Increment operation
• If we increment a pointer by 1, the pointer will start pointing to the immediate
next location. This is somewhat different from the general arithmetic since the
value of the pointer will get increased by the size of the data type to which the
pointer is pointing.
• new_address= current_address + i * size_of(data type)
Where i is the number by which the pointer get increased.
• For Example: If an integer pointer that stores address 1000 is incremented, then
it will increment by 2(size of an int) and the new address it will points to 1002.
While if a float type pointer is incremented then it will increment by 4(size of a
float) and the new address will be 1004.
Decrement operation

• If we decrement a pointer, it will start pointing to the previous location.


• new_address= current_address - i * size_of(data type)
• For Example: If an integer pointer that stores address 1000 is
decremented, then it will decrement by 2(size of an int) and the new
address it will points to 998. While if a float type pointer is decremented
then it will decrement by 4(size of a float) and the new address will be 996.
Addition
• When a pointer is added with a value, the value is first multiplied by the size of data type and then added to the
pointer.
• new_address= current_address + (number * size_of(data type))
• Example: #include<iostream>
Output:
using namespace std; Address of p variable is 3214864300
After adding 3: Address of p variable is 3214864312
int main()
{
int number=50;
int *p; //pointer to int
p=&number; //stores the address of number variable
cout<<"Address of p variable is \n“<<p;
p=p+3; //adding 3 to pointer variable
cout<<"After adding 3: Address of p variable is \n“<<p;
return 0;
}
Subtraction
• Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an
address.
• new_address= current_address - (number * size_of(data type))
• Example: #include<iostream>
using namespace std;
int main()
{
int number=50;
int *p; //pointer to int
p=&number; //stores the address of number variable
cout<<"Address of p variable is \n“<<p;
p=p-3; //subtracting 3 from pointer variable
cout<<"After subtracting 3: Address of p variable is “<<p;
return 0;
Output: }
Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
Subtraction of two pointers
• The subtraction of two pointers is possible only when they have the same data
type. The result is generated by calculating the difference between the addresses of
the two pointers and calculating how many bits of data it is according to the
pointer data type. The subtraction of two pointers gives the increments between the
two pointers.
• For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1016) are
subtracted. The difference between address is 16 bytes. Since the size of int is 2
bytes, therefore the increment between ptr1 and ptr2 is given by (16/2) = 8.

You might also like