# Pointer Arithmetic and Function Pointer.

Provide training in Advanced C++ and working as a software Engineer since 2003
Published:
Updated:
Summary:
This tutorial covers some basics of pointer, pointer arithmetic and function pointer.

What is a pointer:
A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of function.

How to Decide?
int x;
Requirement is :  I would like to store address of x in another variable. How to do it?

My approach would be, which type of variable is it?
Here it's int. So I can consider int*.
int* p = &x; // p is a pointer to an integer
If we want to store address of char variable, then we'd consider char*.
If we want to store address of float variabe, then we'd consider float*.
Likewise if you want to store addess of a variable of type myStruct then you have to use myStruct*
Example:
``````     struct myStruct {
int x;
float y;
char z;
};
struct myStruct StructVar; // myStructObj is a variable of type myStruct
// I want to store address of variable StructVar.
struct myStruct* p = & StructVar; // p is a pointer to a variable of type myStruct i.e StructVar here
``````

Requirement is : I am having 5 integers, I want to store address of all integers in a single variable.
Soln:
Consider array of 5 integer pointers.
Example:
``````      int a=1, b=2, c=3, d=4, e=5;
int* p = {&a, &b, &c, &d, &e}; // Here p is an array of 5 integer pointers.
// Print all the elements.
int i;
for( i = 0; i < 5; ++i) {
printf("%d ", *p[i]);
}
``````
Requirement: I want to store address of function Fun in a variable.
void Fun(); // prototype of function Fun

How to achieve it?
What is Fun?- Fun is a function.
I want to store address of this fuction Fun.
So I have to consider pointer though I am storing address.
Which type of pointer? Is it int/char/float? No... No... No...
Then what?- Though I'm not going to store address of int/char/float in a variable, I can't use int/char/float pointer. Rather I am going to store address of function, So I have to use function pointer.

How to define?
P is a pointer to the function which accepts nothing and returns nothing.
void (*p)();

if prototype of fun is
int fun(int,int); then I can declare the function pointer as follows.
int (*p)(int, int);

Example: (How to call a function using function pointer)
1.Function pointer without argument

``````void fun() {
printf("Void function\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
void (*p)(); // p is a pointer to a function which accepts nothing and returns nothing.
p = fun; // Name of function gives address of the function. It is same as &func
p(); // Calling function fun() using function pointer
return 0;
}
``````
2. Function pointer with argument.
``````int add(int x, int y) {
return x+y;
}

int _tmain(int argc, _TCHAR* argv[])
{
int (*q)(int,int); // q is a pointer to function which receives first argument as int,  2nd arg as int and returns an integer.
q = &add; // only name of function add is also valid. Just for demonostation purpose I kept &add
printf("Sum = %d", q(10, 20));
return 0;
}
``````
• Function pointers are used in callbacks.
• qsort() uses function pointer.(I have not shown here. but you can explore on it. It's a library function).

Pointer arithmetic:
pointer + number = pointer
number + pointer = pointer
pointer + pointer = invalid
pointer - pointer = number

Why SO?
1. In real life, if I would ask where Mr. Bean stays? (Lets he stays next to your house) Your reply may be "next to my house", meaning you are telling me to go to next address. So address + number gives another address, likely number+address.
Which is
Pointer + Number = Pointer
Number + Pointer = Pointer

2. If I ask "what is your address?", the answer might be "Plot No: 66". "What's Mr. Bean's address?" The answer might be "Plot No: 68". If we add the two addresses, would we get anything meaningful? Answer is No No No. We may get another junk address which is none of our interest. So Pointer + Pointer = INVALID

However, if we subtract two addresses, here it'd be 68-66 = 2. Is it meaningful??? Answer: Yes.... Yes.... Yes.... It tells the distance/offset between your house and Mr. Bean's house. (We may say "After one house, you will find Mr. Bean's location). So Pointer - Pointer = Number.

Example:
``````void Display(const char* p) {
while(*p != '\0') {
putchar(*p);
p = p + 1; // pointer + number = pointer
}
}
int _tmain(int argc, _TCHAR* argv[]) {
Display("Subrat");
Display("\tKumar");
Display("\tSwain\0DontPrint\n");
return 0;
}
``````

Here p is pointing to S of string "Subrat". p+1 points to u means to next charector address.
p+2 points to b and likewise, so pointer + number = pointer.

Example 2:
Reverse a string
``````void strrev(const char* p) {
int len = strlen(p);
p = p + len; // Here we are making the pointer to point to last charecter.

do {
putchar(*p);
p = p - 1;
} while(len--);
}

int _tmain(int argc, _TCHAR* argv[])
{
strrev("Subrat");
return 0;
}
``````

We are decrementing the pointer, moving the pointer backward and printing the corresponding characters. p = p - 1; is doing the job for us, so pointer - number = pointer.

Example 3: // Calculate no of characters without using strlen and any counter
``````int StrLen(const char* p) {
char* q = (char*)p;

while(*q != NULL) {
++q;
}
// Now q will point to last charecter
// p is pointng to first charecter
// We are interested in calcuating no of chars.
return (q - p);    // POINTER - POINTER = NUMBER
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", StrLen("Subrat"));
return 0;
}
``````

POINTER - POINTER = NUMBER

Note:
POINTER = POINTER + 1
How much of distance it will travel?
Incrementing a pointer by value 1 would pointer to next item location. (It depends upon type of the item)
Incrementing integer pointer by 1, increments it by value 4. (If size of int is 4bytes)
Incrementing Character pointer by 1, increments it by value 1.(Size of char is 1 byte)
Incrementing float pointer by 1, increments it by value 4.(Size of float is 4 bytes)
Incrementing double pointer by 1, increments it by value 8.
Incrementing a structure pointer by 1, increments it by value = Sizeof  that structure

Example:
``````int i = 10;
int* p = &i; // Let's say &i = 100
// p = 100 as &i = 100
p++; // Now p= p+1 = 100 + 1*sizeof(int) = 100 + 4 = 104;
p++; // Now p = 104 + 4 = 108
``````

``````char c = 'a';
char* p = &c; // Let's say &c = 100
p++; // p = p +1 = 100 + 1*sizeof(char) = 100 + 1 = 101;
p++; // p = 101 + 1 = 102

double d = 10.5;
double* p = &d; // Lets say &d = 100
p++; // p = p + 1 = 100 + 1*sizeof(double) = 100 + 8 = 108
p++; // p = 108 + 8 = 116
``````

``````struct Demo {
double a;
int b;
};
struct Demo* p;
p++; // it'll increment p by value(8+4 = 12)
``````

In general we can say
POINTER = POINTER + n
= POINTER + n * sizeof(TYPE OF POINTER)

If type of pointer is integer and n = 4, (Assume p = 100)
p = p + 4 = 100 + 4*sizeof(int) = 100 + 4*4 = 100 + 16 = 116

In mathematics, we say 100+1 = 101, but in programming like C/C++, we can say if 100 is an address then answer would be - it may, but it may not.

Why so?
The answer is simple. In real world, if you say an Elephant can step like an Ant, can he do it? Really, it's not possible and vice versa is also applicable. In the same way our programming is also following. An int can't walk like char, char can't walk like float and so on.
1
2,892 Views
Provide training in Advanced C++ and working as a software Engineer since 2003