Monday, 29 May 2017

Pointer Arithmetic ~ GNIITHELP

Pointer Arithmetic

Pointer arithmetic is very important to understand, if you want to have complete knowledge of pointer. In this topic we will study how the memory addresses change when you increment a pointer.

16 bit Machine ( Turbo C )

In a 16 bit machine, size of all types of pointer, be it int*, float*, char* or double* is always 2 bytes. But when we perform any arithmetic function like increment on a pointer, changes occur as per the size of their primitive data type.
Size of datatypes on 16-bit Machine :
Type
Size(bytes)
int or signed int2
char1
long4
float4
double8
long double10

Examples for Pointer Arithmetic

Now lets take a few examples and understand this more clearly.
int* i;
i++;
In the above case, pointer will be of 2 bytes. And when we increment it, it will increment by 2 bytes because int is also of 2 bytes.

float* i;
i++;
In this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 4 bytes because float is of 4 bytes.

double* i;
i++;
Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 8 bytes because its data type is double.

32 bit Machine ( Visual Basic C++ )

The concept of pointer arithmetic remains exact same, but the size of pointer and various datatypes is different in a 32 bit machine. Pointer in 32 bit machine is of 4 bytes.
And, following is a table for Size of datatypes on 32-bit Machine :


Type
Size(bytes)
int or signed int4
char2
long8
float8
double16

No comments:

Post a Comment