Skip to main content

Pointer Arithmetic

Increment and Decrement

When we increment an integer pointer by1, it increments by sizeof(int). 

Example: 

int i=20; 
int *ptr=&i; 
i ptr ptr++ ptr 
 20 1000 1002
 1000 2058 or ++ptr 2058 

If a pointer is decremented by 1, it is decremented by the size of the data item it points to. 

When a pointer is incremented by 1 the new value will be (current address in pointer) + sizeof(data_type) 

Pointer Arithmetic Continued … 

Addition and Subtraction


‘C’ allows integers to be added to or subtracted from pointers. When an integer value i is added to a pointer, the new address in the pointer will be 
(current address in pointer) + i* sizeof(data type) 

Example:

int *ptr1, n; 
ptr1=&n; 
ptr1=ptr1+3; 

This code will increment the contents of ptr1 by 3*sizeof(int). If the address in the pointer is 1000, the new address will be 1006. The same concept applies for decrementing a pointer.

Pointer Arithmetic Continued … 

Differencing


Differencing is the subtraction of two pointers. The difference between two pointers indicates how far apart they are. It gives the number of elements between the two pointers. The result is: 
(address in pointer1 – address in pointer2) / sizeof(data_type) 

Example: 

int n,*p,*q; 
p=&n; //suppose address is 1000 
q=p+2; //q now has 1004 
printf(“%d”,q-p); // (1004-1000)/sizeof (int)  
This code will yield a value of 2 even though numerically q and p differ.

Summary of pointer operations


Assignment


The value assigned should be an address  

Indirection

Getting the value stored at an address.

Address of

The & operator used with a variable (or pointer) gives its address. 

Increment

 Incrementing a pointer by 1 adds sizeof(datatype) to the address in the pointer. 
 

Decrement

Decreasing a pointer by 1 subtracts sizeof(datatype) from the address. 

Addition and Subtraction

Adding or subtracting an integer i from a pointer increases/decreases the address by i*sizeof(datatype). 

Differencing

Subtraction of two pointers gives the number of elements between two pointers.
Precedence of & and * operators & and * are unary operators. They have right to left associativity. 

& and * are unary operators. They have right to left associativity. 

Example 1 : 
++*ptr 
Example 
int n=10,*ptr; 
int n=10,*ptr; 
ptr=&n; 
ptr=&n; 
printf(“%d”,++*ptr); 
printf(“%d”,++*ptr); 
Here it will fetch the value at ptr first and then increment it hence 
Here it will fetch the value at ptr first and then increment it hence output will be 11. 
output will be 11. 
 ++ *ptr => ++(*ptr) 
 ++ *ptr => ++(*ptr) 
 =>++(*1058) 
=>++(*1058) 
=>++(10) 
=>++(10) 
=>11 
=>11 
 n ptr ++*ptr ptr n 
 n ptr ++*ptr ptr n 
10 1058 1058 11 
1058 2002 2002 1058
1058 2002 2002 1058 

Example 2: *++ptr 
int n=10,*ptr; 
ptr=&n; 
printf(“%d”,*++ptr); 
Here ++ will be done first. This increments ptr by sizeof(int). Then the value pointed to by the address in ptr will be accessed. *++ ptr => *(++ptr) 
=>*(++1058) 
=>*(1060) 
=>data_at_1060 
n ptr *++ptr ptr Refers  
10 1058 1060 data1058 2002 2002 to 1060 

Example 3: *ptr++ 
Here also, ++ will be performed first. But since it is post-increment, it will first fetch the value pointed to and then increment ptr. 
Relationship between Arrays and Pointers.

Comments

Trending⚡

Happy birthday Hardik Pandya | In C programming

  Happy birthday Hardik Pandya . Now you are  28 years old. Great achievement you have. Let's we want to talk more about Hardik pandya. He is great cricketer. Pandya is awesome. In this Blog Post we are going to wish pandya " Happy birthday using C program". Let's tune with us till end. Now we have to wish pandya, so we are going to use printf () function printing message to pandya as " Happy birthday Hardik pandya Now you are 28 years old". Hardik pandya was born on 11 October in 1993. Now we are going to declare a variable called as current_age = 2021 - 1993. It calculate current age Of Hardik pandya. See the "Happy birthday pandya" using c programming. If you liked this Blog Post then don't forget to share with your computer science learning friends. Once again " Happy birthday Hardik Pandya sir". Read also Happy Rakshabandhan wish using C program Friendship day 2021 greetings in C

Understanding link.click() in JavaScript

Hey! Today i am going to share with you how to use link.click() function in javascript As a JavaScript developer, you may come across the need to programmatically click a link on a web page. The link.click() method is a commonly used way to do this, and it is important to understand how it works and when to use it. What is link.click()? link.click() is a method that can be used to simulate a click on a link element in JavaScript. It is typically used when you want to trigger a link click event programmatically, rather than requiring the user to physically click the link. How to use link.click() Using link.click() is relatively straightforward. First, you need to select the link element you want to click using a DOM selector such as getElementById() or querySelector(). Then, you can call the click() method on the link element to simulate a click. Here is an example: // select the link element const myLink = document.getElementById('my-link'); // simulate a cl...

Best course Recommendation of IT field in 2023

Recommend me best course 🎉50% Discount on registration today itself 🎉! Join Course Now In today's fast-paced digital age, staying updated with the latest technology trends is crucial for professionals looking to advance in their careers. With technology rapidly evolving, it is important to continuously upskill and gain new knowledge to stay relevant in the job market. In 2023, there are several courses in technology that are gaining popularity and are highly recommended for professionals seeking to enhance their skills in data science, analytics, and digital marketing. Data Science: Data science is an interdisciplinary field that combines statistical analysis, machine learning, and computer science to extract insights and knowledge from data. With the increasing amount of data available, data scientists are in high demand across various industries. The best courses in data science for 2023 are th...

Best Programming Languages to Learn in 2022-2023 | Programming Guide For Beginners

Hey everyone ! Today I am going to share with you which programming language you must learn and why you must learn in year 2022. There are so many programmers,coders, computer science students have a Question in their mind , Which programming language I should have to learn ? I am going to answer this very Important Question. Please tune with me till the end. This article will be very helpful for every computer science and coding enthusiast. Let's get started. The world of programming is dynamic. It means the need for language changes according to duration. Every year we see a new language. Everyone gives us feedback on which language we have to learn. But it's not about which language you must've learn , I would like to ask you whats your purpose is in learning a programming language. Why do you want to learn a programming language ? Well you have answers like I want to create softwares , Applications and websites, decentralised apps, cryptocurrency, blockchai...

What is programming explained in simple words

Hi my dear friends today in this blog post we are going to know what programming is? In simple words I will explain to you programming. Nowadays we are watching real life use of programming. How computers learn to speak, talk and do the specified complex task for us. We are all keen to know what is exactly programming? Programming is the process of creating instructions that a computer can understand and execute. These instructions, also known as code, are written in a programming language that is specific to the task at hand. The history of programming can be traced back to the mid-20th century, with the development of the first electronic computers. The first programming languages were known as machine languages, which were specific to a particular type of computer. As computers became more sophisticated, high-level programming languages were developed, such as FORTRAN and COBOL, which were easier for humans to read and write. These languages allow programmers to write code t...

Define a macro EQUALSTR which accepts two strings and compare equality of both string

Define a macro EQUALSTR which accepts two strings and compare equality of both string #include<stdio.h>  #include<string.h>  #define EQUALSTR(str1,str2) strcmp(str1,str2)?0:1  void main()  {  char str1[10],str2[10];  printf("\n Enter the two strings: ");  gets(str1);  gets(str2);  if(EQUALSTR(str1,str2))  printf("\n Strings are equal");  else  printf("\n Strings are not equal");  } Also Read C program to find largest of two numbers using Structure Predefined Macro program Macros vs Functions Preprocessor Syntax Task Performed by Preprocessor