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⚡

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...

How to Create Studio Ghibli-Style AI Images on ChatGPT for Free

How to Create Studio Ghibli-Style AI Images on ChatGPT for Free AI-generated art is making waves across the internet, captivating audiences with stunning, ethereal visuals inspired by the iconic animation style of Studio Ghibli . These AI-crafted images, from dreamy landscapes to expressive characters, reflect the timeless magic of Hayao Miyazaki ’s beloved films such as Spirited Away , My Neighbor Totoro , and Howl’s Moving Castle . Thanks to recent advancements in AI technology, particularly OpenAI ’s latest ChatGPT update, users can now create their own Studio Ghibli-inspired illustrations effortlessly by entering simple text prompts. This exciting feature is transforming digital art creation and making it accessible to both professionals and beginners. In this article, we’ll guide you through creating Ghibli-style AI images using ChatGPT and explore free alternatives for users who don’t yet have access to this feature. Ghibli AI generator free Step-by-Step Guide: How to Crea...

Value Model vs Reference Model

Value Model vs Reference Model In programming languages, two different models are used for variables.  These are:  Value Model  A variable contains a value. The name of the variable gives its value.  Reference Model A variable contains (say y) refers to another variable (say x) with a value. The variable ‘y’ is used to access the value of ‘x’ indirectly.  The ‘C’ language is based on value model. However, by using pointers, we can implement the reference model. The pointer is used to access the value of a variable indirectly. Also Read Static and Dynamic Memory Allocation Memory Leak and Dangling Pointer Memory Allocation for 2D Array Dynamic Memory Allocation Pointer Constant and Constant Pointer Pointer Declarations and their Meanings Functions and Pointers Initializing Pointer to Pointer Pointer to Pointer Multiple Indirection Relationship between Array and Pointer Pointer to Array Pointer Arithmetic Types of Pointer Illustrat...

How to write programs in Bhai language

Bhai Language Bhai language is fun Programming language , with this language you can makes jokes in hindi. Bhai language written in typescript. It's very funny , easy and amazing language. Keywords of this language written in Hindi . Starting and ending of the program Start program with keyword " hi bhai " and end with " bye bhai ". It's compulsory to add this keyword before starting and end on the program. You write your programming logic inside this hi bhai and bye bhai . How to declare variables in Bhai language We use " bhai ye hai variable_name" keyword for declaring variables. In javascript we use var keyword for declaring variables but here you have to use " bhai ye hai " keyword. If you are declaring string then use " " double quotes. You can use Boolean variable like sahi and galat for true and false . How to print output in Bhai language You have to use " bol bhai " keyword for ...

ChatGPT Now Allows Free Users to Create Ghibli-Style AI Images – Here’s How

OpenAI has finally expanded its native image generation feature to free ChatGPT users, allowing them to transform images into stunning Studio Ghibli-style artwork . While the company has yet to make an official announcement, multiple tests using free ChatGPT accounts confirm that the feature is now accessible without requiring a paid subscription. Ghibli AI Images Now Available for Free Users Previously, OpenAI restricted its image generation capabilities to ChatGPT Plus, Pro, and Team users. This led free-tier users to seek alternatives like xAI’s Grok and Google’s Gemini . However, these tools often lacked the same level of detail and artistic refinement as OpenAI’s model. Now, with the rollout extending to free users, everyone can experience the magic of Ghibli-style AI transformations. How to Create Ghibli-Style AI Images with ChatGPT If you want to turn your photos into Ghibli-inspired masterpieces, follow these simple steps: Visit ChatGPT – Open the ChatGPT website or...

Quiz tells you what type of wife you want

What Type of Wife are You Looking For? Attend this quiz and know your wife expections Quiz reveals type of wife you expect, lets answer carefully... Personality Questions: 1. What type of personality are you looking for in a wife? Quiet and reserved Outgoing and social Intelligent and witty 2. What type of sense of humor are you looking for in a wife? Dry and sarcastic Witty and clever Playful and silly 3. What type of interests are you looking for in a wife? Intellectual and educational Creative and artistic Athletic and outdoorsy Values Questions: 4. What type of family values are you looking for in a wife? Traditional and conservative Open-minded and progressive Balanced and equal 5. What type of political views...