Anda di halaman 1dari 7

Use of a Pointer

Pointers store address of variables or a memory location.

Two Operators are necessary to work with pointers.

1. &(ampersand)- Unary Operator that returns the address of a variable.


2. *(Asterisk)- Unary Operator- To declare a pointer variable.
- To access the value stored in the memory location.

Program to demonstrate the use of ampersand operator which returns the address of a
variable.

#include <stdio.h>
int main()
{
int x; // The program returns different addresses in different run time.//

// Prints address of x
printf("%p", &x);

return 0;
}

Program to demonstrate the use of asterisk operator (Declaration of pointer variable)

// C program to demonstrate declaration of


// pointer variables.
#include <stdio.h>
int main()
{
int x = 10;
int *ptr;
// & operator before x is used to get address of x. The address of x is assigned to ptr.
ptr = &x;
return 0;
}

If we want to print something in order to understand the working of this operator,


Let us add ; Printf(“%d”,x); Output: 10;
Printf(“%d”,*x); Output: Error
Printf(“%d”,&x); Output: No Output
Printf(“%d”,&ptr); Output: No Output
Printf(“%d”,*ptr); Output:10
Printf(“%d”,ptr); Output: Error
Printf(“%d”,&ptr); Output: Error
Printf(“%p”,x); Output: Error ;
Printf(“%p”,*x); Output:
Printf(“%p”,&x); Output: 0x7fffee7bbfc4
Printf(“%p”, ptr); Output: 0x7fff4e03bec4 ;
Printf(“%p”,&ptr); Output: 0x7ffc962e63a8
Printf(“%p”,* ptr); Output: Error

Program to demonstrate the use of asterisk operator (To access the value in the address)

#include <stdio.h>

int main()
{
// A normal integer variable
int Var = 10;

// A pointer variable that holds address of var.


int *ptr = &Var;

// This line prints value at address stored in ptr.


// Value stored is value of variable "var"
printf("Value of Var = %d\n", *ptr);

// The output of this line may be different in different


// runs even on same machine.
printf("Address of Var = %p\n", ptr);

// We can also use ptr as lvalue (Left hand


// side of assignment)
*ptr = 20; // Value at address is now 20

// This prints 20
printf("After doing *ptr = 20, *ptr is %d\n", *ptr);

return 0;
}
OUTPUT
Pointer Expressions and Arithmetic
A pointer can be
 Incremented and decremented
 Added and Subtracted
Note: Pointer arithmetic is meaningless unless performed on an array.
1. Pointer Addition
It is perfectly legal if we add an integer variable with a pointer.

final value = (address) + (number * size of data type)


Example :
#include<stdio.h>
int main(){
int *ptr=(int *)1000;
ptr=ptr+3;
printf("New Value of ptr : %p",*ptr); // &ptr can also be used
return 0;
}
Output:

Explanation:

Suppose if the declaration goes of like this: int *ptr=(int *)1000;


Where the memory location is assumed as 1000. Therefore,
Ptr= ptr+3*size of int;
Ptr=1000+3*2;
Ptr=1006.
2. Pointer Subtraction
Formula: new_address = (current address) - i * size_of(data type)
Decrementing a pointer makes
1. An integer data will cause its value to be decremented by 2
2. Memory requirement to store integer vary compiler to compiler
Example:
#include<stdio.h>

int main(){

float *ptr1=(float *)1000;


float *ptr2=(float *)2000;

printf("\nDifference : %d",ptr2-ptr1);

return 0;
}
Output:
Difference : 250
ptr2 - ptr1 = (2000 - 1000) / sizeof(float)
= 1000 / 4
= 250
Subtraction of two numbers using pointers
#include<stdio.h>
void main()
{
int num1,num2,sub,*p1,*p2;
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
p1=&num1;
p2=&num2;
sub=*p1-*p2;
printf("\n\nDifference of %d and %d is %d",*p1,*p2,sub);
}
Output:

Why addition of two pointers is not allowed?


Addition of two pointers will add two addresses and might give an address which might be so
large that it is outside the range of our 32 bit or 64 bit system of addresses in contiguous memory
locations. So it is not a valid operation, whereas you can add numeric values to the pointer to
make it point to an address block which is that numeric value*sizeof(data type) number of
blocks away. So adding a number to a pointer is valid, while addition of pointers is not.
Subtraction of pointers might give a valid address location within the range of addresses so it's
allowed.

Simple Program to demonstrate the concept of Pointer Arithmetic

// C++ program to illustrate Pointer Arithmetic


// in C/C++
#include <bits/stdc++.h>

// Driver program
int main()
{
// Declare an array
int v[3] = {10, 100, 200};

// Declare pointer variable


int *ptr;
// Assign the address of v[0] to ptr
ptr = v;

for (int i = 0; i < 3; i++)


{
printf("Value of *ptr = %d\n", *ptr);
printf("Value of ptr = %p\n\n", ptr);

// Increment pointer ptr by 1


ptr++;
}
}
Output

Array Names as Pointers

An array name acts like a pointer constant. The value of this pointer constant is the address of the
first element. For example, if we have an array named val then val and &val[0] can be used
interchangeably.

Program

// C++ program to illustrate Array Name as Pointers in C++

#include <bits/stdc++.h>
using namespace std;

void geeks()
{
// Declare an array
int val[3] = { 5, 10, 20 };

// Declare pointer variable


int *ptr;

// Assign address of val[0] to ptr.


// We can use ptr=&val[0];(both are same)
ptr = val ;
cout << "Elements of the array are: ";
cout << ptr[0] << " " << ptr[1] << " " << ptr[2];

return;
}

// Driver program
int main()
{
geeks();
return 0;
}

Output:

Pointer to a pointer (Double Pointer)

Pointer is a variable that holds the memory address of another variable. The first pointer is used
to store the address of second pointer. That is why they are also known as double pointers.

Example:

int main()
{
int var = 789;

// pointer for var


int *ptr2;

// double pointer for ptr2


int **ptr1;

// storing address of var in ptr2


ptr2 = &var;

// Storing address of ptr2 in ptr1


ptr1 = &ptr2;

// Displaying value of var using


// both single and double pointers
printf("Value of var = %d\n", var );
printf("Value of var using single pointer = %d\n", *ptr2 );
printf("Value of var using double pointer = %d\n", **ptr1);

return 0;
}

Anda mungkin juga menyukai