14.10 – Pointer arithmetic – Scale factor

by on January 23, 2018

Pointer arithmetic

In case of primitive types adding 1 to the variable results incrementing of its value by 1 and subtracting 1 to the variable results decrementing of its value by 1.

Since pointer is not a normal variable adding 1 to the pointer results next variable’s address. Subtracting 1 to the pointer results previous variables address.

Now we will observe the same with an example. Say for example a series of three variables of short type are declared and a pointer is stored with the address of a variable

```short a=10,b=20,c=30;
short *p=&c;
```

Here p is the pointer to the variable c, holds the address of c 65520. Now p+1 gives the address of next variable b 65522 and p+2 gives the address of next variable a 65524.

```#include<stdio.h>
int main()
{
short a=10,b=20,c=30;
short *p=&c;
printf("\np     %u",p);
printf("\np+1 %u",p+1);
printf("\np+2 %u",p+2);
return 0;
}
```

Output:
p     65520
p+1 65522
p+2 65524

By this we understand that, value of short pointer increments by 2 on adding 1 to the pointer.

If we take a float pointer then increments by 4 on adding 1 to the pointer.

```#include<stdio.h>
int main()
{
float a=10.34,b=20.56,c=30.44;
float *p=&c;
printf("p %u",p);
printf("\np+1 %u",p+1);
printf("\np+2 %u",p+2);
return 0;
}
```

Output:
p 65514
p+1 65518
p+2 65522

Scale factor

If we conclude the above observation, short pointer increments by 2 on adding 1, float pointer increments by 4 on adding 1 and char pointer increments by 1 on adding 1 that is pointer is incrementing by its type size on adding 1

Scale factor is the quantity of increment in the value of a pointer on adding 1

 Data Type Scale factor short 2 int 2/4 long 4 long long      (only in gcc) 8 char 1 float 4 double 8 long double 10/12 void 0

Arithmetic operations on pointers:

• Operations like +, -, ++ and –- can be performed on pointers
• Can’t perform *, / operations
• Arithmetic operations on pointers can only performed with integers but, not with any other types

Let us take a series of short variables and assign the address of first variable to the pointer. Now the pointer is capable to access all the variables using pointer arithmetic

```short a=10,b=20,c=30,d=40,e=50;
short *p=&a;
```

Here all the short type of variables are declared in a sequence. The memory allocation of which are allocated in consecutive memory allocations like an array
“p” is the pointer to the first variable.
Because scale factor of short type is 2 by decrementing the value of “p” by 1 we can access all the variables one by one.

```#include<stdio.h>
int main()
{
short a=10,b=20,c=30,d=40,e=50;
short *p=&a;
printf("Values of all the variables in a sequence:\n");
printf("%d",*(p-0));
printf("%5d",*(p-1));
printf("%5d",*(p-2));
printf("%5d",*(p-3));
printf("%5d",*(p-4));
return 0;
}
```

Output:
Values of all the variables in a sequence:
10   20   30   40   50
The same program can be written as

```#include<stdio.h>
int main()
{
short a=10,b=20,c=30,d=40,e=50,i;
short *p=&a;
printf("Values of all the variables in a sequence:\n");
for(i=0;i<5;i++)
printf("%5d",*(p-i));
return 0;
}
```

Output:
Values of all the variables in a sequence:
10   20   30   40   50

Previous post:

Next post: