Pointers Arithmetic
- Increment
- Decrement
Note : We cannot add,multiply,divide,subtract Two pointers.
but, We can add and subtract a number from an Pointer.
Increment(++)
Incrementing an pointer:
#include<stdio.h>
void main()
{
int a = 10; //integer variable
int *p; //Pointer of level one
p = &a; //assigning the address of variable a
//to a pointer p of level one
printf("Before incrementing p: %u",p);
p++; //incrementing the pointer
printf("\nAfter Incrementing p: %u",p);
}
#include<stdio.h> void main() { int a = 10; //integer variable int *p; //Pointer of level one p = &a; //assigning the address of variable a //to a pointer p of level one printf("Before incrementing p: %u",p); p++; //incrementing the pointer printf("\nAfter Incrementing p: %u",p); }
Output
Before incrementing p: 6356744
After Incrementing p: 6356748
After Incrementing p: 6356748
Formula:
ptr = ptr + sizeof(datatype_pointer)
ptr = ptr + sizeof(datatype_pointer)
This is what happen when we incremented the Pointer ptr:
ptr = 6356744 + sizeof(int)
ptr = 6356744 + 4
ptr = 6356748
ptr = 6356744 + sizeof(int) ptr = 6356744 + 4 ptr = 6356748
- The concept of pointer arithmetic remains exact same, but the size of pointer and various datatypes is different in a different bits of machine.
- But for now the Size of integer pointer is 4 because the
c language which we are studying in that size of integer variable is 4 bytes - So, to know what is the size of data types in your machine than you can use sizeof() function which returns the size of datatypes
In above case pointer was of integer data type,but what if it was float or char type.
- Nothing will change just it will increment the pointer by the respected size of the data type
- If pointer is of char type suppose address of char pointer p is 5000 than if we increment it then it will give p = 5001
- Similarly , if Pointer is of Float type , suppose p = 5000 than if we increment the pointer than it give p = 5004
Incrementing the float Pointer:
#include<stdio.h>
void main()
{
float a = 10.1; //float variable
float *p; //Pointer of level one
p = &a //assigning the address of variable a
//to a pointer p of level one
printf("Before incrementing p: %u",p);
p++; //incrementing the pointer
printf("\nAfter Incrementing p: %u",p);
}
#include<stdio.h> void main() { float a = 10.1; //float variable float *p; //Pointer of level one p = &a //assigning the address of variable a //to a pointer p of level one printf("Before incrementing p: %u",p); p++; //incrementing the pointer printf("\nAfter Incrementing p: %u",p); }
Output
Before incrementing p: 6356744
After Incrementing p: 6356748
After Incrementing p: 6356748
This is what happen when we incremented the float Pointer p:
p = 6356744 + sizeof(float)
ptr = 6356744 + 4
ptr = 6356748
p = 6356744 + sizeof(float) ptr = 6356744 + 4 ptr = 6356748
Incrementing the Char Pointer:
#include<stdio.h>
void main()
{
char a = 'A'; //char variable
char *p; //Pointer of level one
p = &a //assigning the address of variable a
//to a pointer p of level one
printf("Before incrementing p: %u",p);
p++; //incrementing the pointer
printf("\nAfter Incrementing p: %u",p);
}
#include<stdio.h> void main() { char a = 'A'; //char variable char *p; //Pointer of level one p = &a //assigning the address of variable a //to a pointer p of level one printf("Before incrementing p: %u",p); p++; //incrementing the pointer printf("\nAfter Incrementing p: %u",p); }
Output
Before incrementing p: 6356747
After Incrementing p: 6356748
After Incrementing p: 6356748
This is what happen when we incremented the char Pointer p:
p = 6356747 + sizeof(char)
ptr = 6356747 + 1
ptr = 6356748
p = 6356747 + sizeof(char) ptr = 6356747 + 1 ptr = 6356748
Decrement(--)
- Its Concept is same as increment(++) just inspite of adding 1 it subtracts one from the pointer and gives the address of previous block of same type.
Decrementing an pointer:
#include<stdio.h>
void main()
{
int a = 10; //integer variable
int *p; //Pointer of level one
p = &a; //assigning the address of variable a
//to a pointer p of level one
printf("Before Decrementing p: %u",p);
p--; //Decrementing the pointer
printf("\nAfter Decrementing p: %u",p);
}
#include<stdio.h> void main() { int a = 10; //integer variable int *p; //Pointer of level one p = &a; //assigning the address of variable a //to a pointer p of level one printf("Before Decrementing p: %u",p); p--; //Decrementing the pointer printf("\nAfter Decrementing p: %u",p); }
Output
Before Decrementing p: 6356744
After Decrementing p: 6356740
After Decrementing p: 6356740
Formula:
ptr = ptr - sizeof(datatype_pointer)
ptr = ptr - sizeof(datatype_pointer)
This is what happen when we Decremented the Pointer ptr:
ptr = 6356744 + sizeof(int)
ptr = 6356744 - 4
ptr = 6356740
ptr = 6356744 + sizeof(int) ptr = 6356744 - 4 ptr = 6356740
Adding and Subtracting a number from the Pointer.
- As you know that how pointer get add and subtract.
- Now we will add or subtract number from a Pointer.
Adding a number from a pointer:
#include<stdio.h>
void main()
{
int a = 5; //int variable
int *p; //Pointer of level one
p = &a; //assigning the address of variable a
//to a pointer p of level one
printf("Before incrementing p: %u",p);
p = p+2; //Adding 2 to pointer p
printf("\nAfter Adding p + 2 = %u",p);
}
#include<stdio.h> void main() { int a = 5; //int variable int *p; //Pointer of level one p = &a; //assigning the address of variable a //to a pointer p of level one printf("Before incrementing p: %u",p); p = p+2; //Adding 2 to pointer p printf("\nAfter Adding p + 2 = %u",p); }
Output
Before incrementing p: 6356744
After Adding p + 2 = 6356752
After Adding p + 2 = 6356752
Formula:
ptr = ptr + number*sizeof(datatype_pointer)
ptr = ptr + number*sizeof(datatype_pointer)
This is what happen when we added 2 to the Pointer p:
p = 6356744 + 2*sizeof(int)
p = 6356744 + 2*4
p = 6356744 + 8
p = 6356752
p = 6356744 + 2*sizeof(int) p = 6356744 + 2*4 p = 6356744 + 8 p = 6356752
- Similarly ,we can Subtract a number from a pointer.
- Whole concept will be same only you have to just subtract a number instead of adding a number
Further Concept:
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
Further Topics:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment