Categories: C

Pointer operations

Just like any other data type, we can perform various arithmetic operations on pointers.

All pointers int, float, long, and double are 2 bytes on a 16-bit machine.

When we perform any arithmetic function, such as increment on a pointer, the size of their primitive data type changes.

Remember the following data type storage sizes in a 16 bit machine:

C Data types / storage SizeRange
char / 1–127 to 127
int / 2–32,767 to 32,767
float / 41E–37 to 1E+37 with six digits of precision
double / 81E–37 to 1E+37 with ten digits of precision
long double / 101E–37 to 1E+37 with ten digits of precision
long int / 4–2,147,483,647 to 2,147,483,647
short int / 2–32,767 to 32,767
unsigned short int / 20 to 65,535
signed short int / 2–32,767 to 32,767
long long int / 8–(2power(63) –1) to 2(power)63 –1
signed long int / 4–2,147,483,647 to 2,147,483,647
unsigned long int / 40 to 4,294,967,295
unsigned long long int / 82(power)64 –1
Pointer Arithmetic Examples:
int* a;
a++;

In the preceding example, the pointer will be two bytes long. When we increment it, it will increase by two bytes because int is also two bytes.

For instance:

#include<stdio.h>
int main()
{

   int  b;
   int  *p;
   int  **ptr;

   b = 10;

   /* take the address of b */   p = &b;

   /* take the address of p using address of operator & */   ptr = &p;

   /* take the value using ptr */   printf("Value of b = %d\n", b );
   printf("Value available at *p = %d\n", *p );
   printf("Value available at **ptr = %d\n", **ptr);
    /* take the address  */   printf("Value of &b = %u\n", &b );
   printf("Value available at p = %u\n", p );
   printf("Value available at ++p = %u\n", ++p );
    printf("Value available at ptr = %u\n", ptr);
    printf("Value available at ++ptr = %u\n", ++ptr);

   return 0;
}
Output:
/tmp/L66gawGDLh.o
Value of b = 10
Value available at *p = 10
Value available at **ptr = 10
Value of &b = 1618367364
Value available at p = 1618367364
Value available at ++p = 1618367366
Value available at ptr = 1618367368
Value available at ++ptr = 1618367380

Here, we can clearly see that the value of ++p and ++ptr is incremented by 2 bytes. Similarly, the value is increased in proportion to their size.

If we used a four-bit machine, the same output would have been incremented by four bytes for integer data.

Note: also read about the Pointer to Pointer in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago