Pointer Expressions and Arithmetic

Rumman Ansari   Software Engineer   2019-03-31   17853 Share
☰ Table of Contents

Table of Content:


In general, expressions involving pointers conform to the same rules as other expressions. This section examines a few special aspects of pointer expressions, such as assignments, conversions, and arithmetic.

Pointer Assignments

You can use a pointer on the right-hand side of an assignment statement to assign its value to another pointer. When both pointers are the same type, the situation is straightforward. For example:

int s = 56;
int *ptr1, *ptr2;

ptr1 = &s;
ptr2 = ptr1;

Program

#include 
int main(void)
{
int s = 56;
int *ptr1, *ptr2;

ptr1 = &s;
ptr2 = ptr1;

/* print the value of s twice */
printf("Values at ptr1 and ptr2: %d %d \n", *ptr1, *ptr2);

/* print the address of s twice */
printf("Addresses pointed to by ptr1 and ptr2: %p %p",ptr1,ptr2);
return 0;
}

Output

Values at ptr1 and ptr2: 56 56
Addresses pointed to by ptr1 and ptr2: 0240FF20 0240FF20 

Pointer Arithmetic

We can perform addition and subtraction of integer constant from pointer variable.

Addition

ptr1 = ptr1 + 2; 

subtraction

ptr1 = ptr1 - 2; 

We can not perform addition, multiplication and division operations on two pointer variables.

For Example: 
ptr1 + ptr2 is not valid

However we can subtract one pointer variable from another pointer variable. We can use increment and decrement operator along with pointer variable to increment or decrement the address contained in pointer variable.

For Example: 
ptr1++; 
ptr2--;

Multiplication

Example:
int x = 10, y = 20, z; 
int *ptr1 = &x; 
int *ptr2 = &y; 
z = *ptr1 * *ptr2 ; 
Will assign 200 to variable z.

Division

there is a blank space between '/' and * because the symbol /*is considered as beginning of the comment and therefore the statement fails.

 
Z=5*-*Ptr2/ *Ptr1; 

If Ptr1 and Ptr2 are properly declared and initialized pointers, then the following statements are valid:

Y=*Ptr1**Ptr2;
Sum=sum+*Ptr1; 
*Ptr2=*Ptr2+10;
*Ptr1=*Ptr1+*Ptr2;
*Ptr1=*Ptr2-*Ptr1;
Pointer arithmetic

if Ptr1 and Ptr2 are properly declared and initialized pointers then, 'C' allows adding integers to a pointer variable.

EX:
int a=5, b=10; 
int *Ptr1,*Ptr2;
Ptr1=&a; 
Ptr2=&b

If Ptr1 & Ptr2 are properly declared and initialized, pointers then 'C' allows to subtract integers from pointers. From the above example,

Pointer arithmetic

If Ptr1 & Ptr2 are properly declared and initialize pointers, and both points to the elements of the same type. "Subtraction of one pointer from another pointer is also possible".

NOTE: this operation is done when both pointer variable points to the elements of the same array.

EX:
P2- P1 (It gives the number of elements between p1 and p2)

Pointer Increment and Scale Factor

We can use increment operator to increment the address of the pointer variable so that it points to next memory location. 
The value by which the address of the pointer variable will increment is not fixed. It depends upon the data type of the pointer variable. 

 For Example: 
 int *ptr; 
 ptr++;
It will increment the address of pointer variable by 2. So if the address of pointer variable is 2000 then after increment it becomes 2002.
Thus the value by which address of the pointer variable increments is known as scale factor. The scale factor is different for different data types as shown below:

Char 

1 Byte

Int

2 Byte

Short int

2 Byte

Long int

4 Byte

Float

4 Byte

Double

8 Byte

Long double

10 Byte

Write a C program to compute the sum of all elements stored in an array Using pointers.

Program

/*program to compute sum of all elements stored in an
array */
#include
#include
main ()
{
		int a [10], i, sum=0,*p;
		printf ("enter 10 elements \n");
		for (i=0; i<10; i++)
		scanf ("%d", & a[i]);
		p = a;
		for (i = 0; i<10; i++)
		{
		sum = sum+(*p);
		p++;
		}
		printf ("the sum is % d", sum);
		getch ();
}

Output

enter 10 elements
1
2
3
4
5
6
7
8
9
10
the sum is  55 

Write a C program using pointers to determine the length of a character String.

Program

/*program to find the length of a char string */
#include 
#include
main ()
{
		char str[20], *ptr ;
		int l=0;
		printf("enter a string \n");
		scanf("%s", str);
		ptr=str;
		while(*ptr!='\0')
		{
		l++;
		ptr++;
		}
		printf("the length of the given string is %d \n", l);
 
 }

Output

enter a string
atnyla.com
the length of the given string is 10
Press any key to continue . . .