C Pointers with assignment operators


In C programming, pointers can be used with assignment operators to modify the values they point to, or to update the addresses they hold. Let's dive into how pointers interact with assignment operators.

Basic Pointer Assignment

Pointer assignment involves assigning the address of a variable to a pointer. This is the fundamental assignment operation for pointers.

Example

int a = 5; int *p = &a; // 'p' now holds the address of 'a'

Here, p is assigned the address of a using the assignment operator (=).

Assignment Operators with Dereferencing

Using dereferencing (*) with pointers allows you to modify the value at the memory address the pointer holds. This can be combined with assignment operators like =, +=, -=, etc., to manipulate data indirectly.

Example

int a = 10; int *p = &a; // 'p' points to 'a' *p = 20; // Assigns 20 to 'a' (through pointer dereferencing) printf("%d\n", a); // Output: 20

In this example, *p refers to the value stored at the address p points to (which is a). By using the assignment operator =, the value of a is updated to 20.

Compound Assignment with Pointers

You can use compound assignment operators (like +=, -=, *=, /=, etc.) with dereferenced pointers to update the value stored in the memory location they point to.

Example

int a = 5; int *p = &a; *p += 10; // Same as: 'a = a + 10' printf("%d\n", a); // Output: 15 *p *= 2; // Same as: 'a = a * 2' printf("%d\n", a); // Output: 30

Explanation:

  • *p += 10: This adds 10 to the value of a through the pointer.
  • *p *= 2: This multiplies the value of a by 2 through the pointer.

Assigning Pointers to Other Pointers

You can also assign one pointer to another. This simply means that the new pointer will point to the same address as the original pointer.

Example

int a = 100; int *p1 = &a; // 'p1' points to 'a' int *p2 = p1; // 'p2' now also points to 'a' printf("%d\n", *p2); // Output: 100 (since 'p2' points to 'a')

In this example, p2 is assigned the value of p1, which means both pointers p1 and p2 point to the same memory address (the address of a).

Example of Combining Assignment Operators with Pointers

#include <stdio.h> int main() { int num = 10; int *ptr = &num; // Assigns the address of 'num' to 'ptr' // Using assignment operators through pointer dereferencing *ptr += 5; // Increment the value of 'num' by 5 printf("Value of num after += 5: %d\n", num); // Output: 15 *ptr -= 3; // Decrement the value of 'num' by 3 printf("Value of num after -= 3: %d\n", num); // Output: 12 *ptr *= 2; // Multiply the value of 'num' by 2 printf("Value of num after *= 2: %d\n", num); // Output: 24 *ptr /= 4; // Divide the value of 'num' by 4 printf("Value of num after /= 4: %d\n", num); // Output: 6 return 0; }

Explanation:

  • *ptr += 5 increases the value of num from 10 to 15.
  • *ptr -= 3 decreases it from 15 to 12.
  • *ptr *= 2 multiplies it by 2, resulting in 24.
  • *ptr /= 4 divides it by 4, resulting in 6.

Pointer Reassignment

Pointers can be reassigned to different addresses during their lifetime. This allows dynamic changes in what the pointer refers to.

Example

int x = 5, y = 10; int *p = &x; // 'p' points to 'x' printf("%d\n", *p); // Output: 5 p = &y; // Reassign 'p' to point to 'y' printf("%d\n", *p); // Output: 10

Important Points

  1. Dereferencing Required for Value Assignment: To modify the value pointed to by a pointer, you must use the dereference operator (*). Without it, you would be changing the pointer itself.
  2. Pointer Reassignment Changes Address, Not Value: Assigning a new address to a pointer (p = &y;) changes what the pointer is pointing to but does not modify the value at the original address.
  3. Pointer Types Must Match: You should only assign pointers of the same type to each other to avoid undefined behavior.

Summary

  • Pointer Assignment: Assign a pointer to point to a variable (int *p = &a).
  • Dereferencing and Assignment: Use *p to access or modify the value at the memory address (*p = value or *p += value).
  • Pointer Reassignment: Change the address a pointer points to (p = &y).
  • Compound Assignment: You can use assignment operators (+=, -=, etc.) with pointers to modify the value indirectly.

Pointers combined with assignment operations give you a powerful tool for directly manipulating data in memory, making C a flexible but challenging language.