C language content

Pointer arithmetic in C is a powerful feature that allows for direct manipulation of memory addresses. Understanding how it works is crucial for efficient and effective use of pointers in C programming. Here is an in-depth look at pointer arithmetic in C:

Basic Concepts

  1. Pointer Definition: A pointer is a variable that stores the memory address of another variable.
				
					int *p; // p is a pointer to an integer

				
			

2. Address and Dereferencing: The & operator is used to get the address of a variable, and the   *  operator is used to access the value at the address stored in a pointer.

				
					int a = 10;
int *p = &a; // p holds the address of a
int value = *p; // value is now 10

				
			

Pointer Arithmetic

Pointer arithmetic involves operations on pointers which manipulate the memory addresses they contain. The primary operations are addition, subtraction, and comparison.

Addition and Subtraction

When you add or subtract an integer to/from a pointer, it is scaled by the size of the type to which the pointer points.

  • Addition:
				
					int arr[5] = {1, 2, 3, 4, 5};
int *p = arr; // p points to arr[0]
p = p + 1;    // p now points to arr[1]
// p + 1 actually means p + 1 * sizeof(int)

				
			

Here, if  ‘p‘ is a pointer to an  ‘int‘, adding 1 to ‘p‘ increments the address stored in ‘p‘ by ‘sizeof(int)‘ bytes.

  • Subtraction:
				
					int *p1 = &arr[2]; // p1 points to arr[2]
int *p2 = &arr[0]; // p2 points to arr[0]
int diff = p1 - p2; // diff is 2

				
			

Subtracting one pointer from another gives the number of elements of the type between the two addresses.

Increment and Decrement

  • Increment: p++ increments the pointer to point to the next element.
				
					int *p = arr;
p++; // p now points to arr[1]

				
			
  • Decrement: p-- decrements the pointer to point to the previous element.
				
					int *p = &arr[2];
p--; // p now points to arr[1]

				
			

Comparison

Pointers can be compared using relational operators. This is useful for iterating over arrays or determining the relative position of elements in memory.

				
					int *p1 = &arr[1];
int *p2 = &arr[3];
if (p1 < p2) {
    // This condition is true
}

				
			

Practical Examples

1. Array Traversal:

				
					int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
for (int i = 0; i < 5; i++) {
    printf("%d ", *(p + i));
}
// Output: 10 20 30 40 50

				
			

2. Pointer Subtraction:

				
					int arr[] = {10, 20, 30, 40, 50};
int *p1 = &arr[1];
int *p2 = &arr[4];
int elements_between = p2 - p1; // elements_between is 3

				
			

3. Dynamic Memory Allocation:

				
					int n = 5;
int *p = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
    p[i] = i * 10;
}
for (int i = 0; i < n; i++) {
    printf("%d ", *(p + i));
}
// Output: 0 10 20 30 40
free(p);

				
			

Rules and Considerations

  • Type Safety: Pointer arithmetic should be done on pointers of the same type. Mixed-type arithmetic can lead to undefined behavior.

  • Memory Boundaries: Ensure pointers do not go out of the bounds of the allocated memory. Accessing memory outside of the allocated range can cause segmentation faults.

  • Void Pointers: Arithmetic on void pointers is not allowed because void pointers do not have a defined size. Cast them to an appropriate type first.

				
					void *vptr;
int *iptr = (int *)vptr;
iptr++; // This is valid

				
			

Understanding these fundamentals allows for effective use of pointers in array manipulation, dynamic memory management, and implementing complex data structures in C.

Scroll to Top