ADVERTISEMENT
ADVERTISEMENT

What is Pointers in C ?

A pointer is a variable that stores the memory address of another variable. In C, a pointer is defined using the * operator, also known as the "dereference" operator.

Pointer Declaration

A pointer is declared by placing an asterisk (*) in front of the variable name. For example, the following declares a pointer to an int:

int *ptr;

Address operator "&"

The address operator "&" is used to obtain the memory address of a variable. For example, the following assigns the memory address of the variable x to the pointer ptr:

int x = 10;
int *ptr;
ptr = &x;

Indirection operator "*"

The indirection operator "*" is used to access the value stored at the memory address stored in a pointer. For example, the following assigns the value of x to the variable y using a pointer:

int x = 10;
int *ptr = &x;
int y = *ptr;

Pointer Arithmetics Operation in C with Examples

In C, there are several pointer arithmetic operations that can be performed:

  1. Incrementing a Pointer: The ++ operator is used to increment a pointer, which moves the pointer to the next memory location. For example, the following increments the pointer ptr:
int x[5] = {10, 20, 30, 40, 50};
int *ptr = x;
ptr++; // Move to the next memory location
printf("%d", *ptr);  // Output: 20
  1. Decrementing a Pointer: The -- operator is used to decrement a pointer, which moves the pointer to the previous memory location. For example, the following decrements the pointer ptr:
int x[5] = {10, 20, 30, 40, 50};
int *ptr = &x[3];
ptr--; // Move to the previous memory location
printf("%d", *ptr);  // Output: 20
  1. Pointer Addition: The + operator is used to add a value to a pointer, which moves the pointer a certain number of memory locations. For example, the following adds 2 to the pointer ptr:
int x[5] = {10, 20, 30, 40, 50};
int *ptr = x;
ptr = ptr + 2; // Move to the next two memory location
printf("%d", *ptr);  // Output: 30
  1. Pointer Subtraction: The - operator is used to subtract a value from a pointer, which moves the pointer a certain number of memory locations. For example, the following subtracts 2 from the pointer ptr:
int x[5] = {10, 20, 30, 40, 50};
int *ptr = &x[3];
ptr = ptr - 2; // Move to the previous two memory location
printf("%d", *ptr);  // Output: 20
  1. Pointer Difference: The - operator can also be used to find the difference between two pointers, which returns the number of memory locations between the two pointers. For example, the following finds the difference between the pointers ptr1 and ptr2:
int x[5] = {10, 20, 30, 40, 50};
int *ptr1 = &x[2];
int *ptr2 = &x[0];
int diff = ptr1 - ptr2;
printf("%d", diff);  // Output: 2

It's important to keep in mind that pointer arithmetic is based on the size of the data type that the pointer is pointing to. For example, if a pointer is pointing to an int, incrementing the pointer will move the memory address by 4 bytes.

 Arithmetic Operations that are not allowed on pointers in C

  1. Dividing a pointer by an integer: Dividing a pointer by an integer is not allowed in C as it doesn't make sense semantically.

  2. Multiplying a pointer by an integer: Multiplying a pointer by an integer is not allowed in C. Pointers are memory addresses, and multiplying an address by an integer value would produce an invalid address.

  3. Modulus of pointer: The modulus operation is not allowed on pointers, it doesn't make sense semantically.

  4. Assigning pointer with non-integral value: Assigning a pointer with a non-integral value is not allowed in C. Pointers must always point to a valid memory address, and non-integral values do not represent valid memory addresses.

  5. Comparing pointers of different data types: Comparing pointers of different data types is not allowed in C, as the size and memory layout of the two data types may be different.

It's important to keep in mind that pointer arithmetic is based on the size of the data type that the pointer is pointing to. For example, if a pointer is pointing to an int, incrementing the pointer will move the memory address by 4 bytes. It is important to follow the rules and restrictions of pointer arithmetic operations to avoid undefined behavior and errors in the program.


ADVERTISEMENT

ADVERTISEMENT