Pointer Arithmetic in C
Pointer Arithmetic in C
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.