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

Pointer Arithmetic in C

The document explains pointer arithmetic in C, detailing operations such as increment, decrement, addition, subtraction, and comparison. It illustrates how pointers can be manipulated to point to different memory locations based on the size of the data type they reference. Additionally, it includes example code snippets demonstrating these concepts and suggests programming exercises related to string and array manipulation using pointers.
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)
3 views13 pages

Pointer Arithmetic in C

The document explains pointer arithmetic in C, detailing operations such as increment, decrement, addition, subtraction, and comparison. It illustrates how pointers can be manipulated to point to different memory locations based on the size of the data type they reference. Additionally, it includes example code snippets demonstrating these concepts and suggests programming exercises related to string and array manipulation using pointers.
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 Arithmetic in C

• Arithmetic operations on the pointers like


addition, subtraction, etc.
• pointer contains the address, the result of an
arithmetic operation performed on the
pointer will also be a pointer if the other
operand is of type integer.
• Following arithmetic operations are possible
on the pointer in C language:
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
Incrementing Pointer in C

• 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.
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;
//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %
u\
n",p); // in our case, p will get incremented by 4
bytes.
return 0;
#include <stdio.h>
void main()
{
int number=50;
int *p;//pointer to int
p=&number;//
stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-1;
printf("After decrement: Address of p variable is
%u \
n",p); // P will now point to the immidiate previous
location.
}
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p=&number;//
stores the address of number variable
printf("Address of p variable is %u \
n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variabl
e is %u \n",p);
return 0;
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//
stores the address of number variable

printf("Address of p variable is %u \
n",p);
p=p-3; //
subtracting 3 from pointer variable
printf("After subtracting 3: Address of p
variable is %u \n",p);
• instead of subtracting a number, we can also subtract
an address from another address (pointer). This will
result in a number. It will not be a simple arithmetic
operation, but it will follow the following rule.
• If two pointers are of the same type,
• Address2 - Address1 =
• (Subtraction of two addresses)/
size of data type which pointer points
• Consider the following example to subtract one
pointer from an another.
#include<stdio.h>
void main ()
{
int i = 100;
int *p = &i;
int *temp;
temp = p;
p = p + 3;
printf("Pointer Subtraction: %d %d = %d",p, te
mp, p-temp);
}
#include<stdio.h>
int addition ();
int main ()
{ int result;
int (*ptr)();
ptr = &addition;
result = (*ptr)();
printf("The sum is %d",result);
}
int addition()
{ int a, b;
printf("Enter two numbers?");
scanf("%d %d",&a,&b);
return a+b;
}
• Write a program to accept string using
character pointer and display it.
• Write a program to display all elements of an
array using pointers.
• Write a program to calculate the square and
cube of an entered number using pointer of a
varaiable containing the entered number.
• Write a program to enter some text and
display the text in reverse order ( Example: “I
am happy” wii be displayed as “happy am I”)
• Write a program to enter text through
keyboard. Convert the first character of each
word in capital and display text.

You might also like