Difference Between Pointer Dereferencing And Address Of Operator In C

  • The unary address operator (&) can be used to determine the memory location of a variable. In other words, it "points to" where the value of a variable is stored in memory.
  • Purpose: The objective is to find the memory address of a variable.
  • Syntax:

It has the following syntax:

Example

int x = 10;
int *ptr = &x;  // 'ptr' stores the address of 'x'

Pointer Dereferencing (*):

  • Pointer dereferencing is the process of retrieving the value stored in the memory location to which a pointer is pointing. Using the dereferencing operator (*), the value at that memory location can be read or modified.
  • Purpose: The goal is to read or change the value held at a specific memory location.
  • Syntax:

It has the following syntax:

Example

int x = 10;
int *ptr = &x;  // 'ptr' stores the address of 'x'

Pointer Dereferencing (*):

  • Pointer dereferencing is the process of retrieving the value stored in the memory location to which a pointer is pointing. Using the dereferencing operator (*), the value at that memory location can be read or modified.
  • Purpose: The goal is to read or change the value held at a specific memory location.
  • Syntax:

It has the following syntax:

Example

int x = 10;
int *ptr = &x;  // 'ptr' points to 'x'
printf("%d\n", *ptr);  // Dereferencing 'ptr' gives the value of 'x' (10)

Key differences between Address-of Operator and Pointer Dereferencing:

There exist distinct disparities between the Address-of Operator and Pointer Dereferencing in the C programming language. A few primary variances include:

Aspect Address-of operator(&) Pointer Dereferencing(*)
Purpose It retrieves a variable's memory address. It changes or accesses the value located at a memory location.
Operand It manipulates an array element or variable It uses a pointer to function.
Result It generates a memory address. It generates the value that is kept in the memory address.
Usage It makes a variable pointer. It uses a pointer to access data.
Expression &x (x's address) *ptr (value in the ptr memory address)

Example Usage:

Example 1:

Let's consider a scenario to demonstrate the Address-of Operator and Pointer Dereferencing in the C programming language.

Example

#include <stdio.h>

int main() {
    int num = 25;        // Declare an integer variable
    int *ptr = #     // Use address-of operator to assign the address of 'num' to 'ptr'

    printf("Address of num: %p\n", &num);  // Address-of operator
    printf("Address stored in ptr: %p\n", ptr);

    printf("Value of num: %d\n", num);
    printf("Value accessed through ptr: %d\n", *ptr);  // Dereferencing the pointer

    // Modify the value of 'num' using the pointer
    *ptr = 50;
    printf("New value of num after modification: %d\n", num);

    // Use a second pointer
    int **ptr2 = &ptr;   // Pointer to a pointer
    printf("Value of num through ptr2: %d\n", **ptr2); // Double dereference

    return 0;
}

Output:

Output

Address of num: 0x7fffb45798f4
Address stored in ptr: 0x7fffb45798f4
Value of num: 25
Value accessed through ptr: 25
New value of num after modification: 50
Value of num through ptr2: 50

Explanation:

  • &num: It gets the address in memory for num.
  • ptr: The memory address that ptr points to is accessed or modified by ptr.
  • **ptr2: To retrieve the value of num, ptr2 shows how to dereference a pointer-to-pointer.
  • Example 2:

Let's consider another example to demonstrate the Address-of Operator and Pointer Dereferencing in the C programming language.

Example

#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30}; // Declare an array
    int *ptr = &arr[0];        // Use address-of operator to point to the first element

    // Print addresses of array elements
    printf("Address of arr[0]: %p\n", &arr[0]);
    printf("Address stored in ptr: %p\n", ptr);

    // Access and modify values using pointer dereferencing
    printf("Value at ptr (arr[0]): %d\n", *ptr);
    *ptr = 15; // Modify the first element
    printf("New value at ptr (arr[0]): %d\n", *ptr);

    // Move the pointer to the next element and access its value
    ptr++; // Pointer arithmetic
    printf("Value at ptr (arr[1]): %d\n", *ptr);

    // Access the last element using pointer arithmetic
    ptr++;
    printf("Value at ptr (arr[2]): %d\n", *ptr);

    return 0;
}

Output:

Output

Address of arr[0]: 0x7ffc4dbc4f3c
Address stored in ptr: 0x7ffc4dbc4f3c
Value at ptr (arr[0]): 10
New value at ptr (arr[0]): 15
Value at ptr (arr[1]): 20
Value at ptr (arr[2]): 30

Explanation:

  • &arr[0]: The first element of the array's memory location is retrieved using &arr[0].
  • *ptr: It retrieves the value that is kept at the memory address that the pointer corresponds to.
  • ptr++: It moves the pointer to the following array element to illustrate pointer arithmetic.
  • (ptr): Array elements are accessed and modified dynamically via pointer dereferences (ptr).
  • Conclusion:

In summary, a solid grasp of the address-of operator (&) and pointer dereferencing () is essential for implementing efficient memory management and dynamic programming techniques in the C language. The & operator is utilized to retrieve the memory location of a specific variable. By using the operator on a pointer, one can access or modify the value stored at the memory address pointed to by the pointer. These operations collaborate to facilitate complex memory structures like double pointers, dynamic data processing, and proficient array manipulation. Proficiency in these principles paves the way for more sophisticated C programming endeavors and deepens our comprehension of memory access and control in low-level programming.

Input Required

This code uses input(). Please provide values below: