C Programs Tutorials | IT Developer
IT Developer

C Programming - C Dereference Pointer



Share with a Friend

C Programming - C Dereference Pointer

C Dereference Pointer

Dereferencing a pointer means accessing the value that the pointer is pointing to. In C, dereferencing is done using the * operator, which allows you to access the memory location the pointer is pointing to and work with the value stored there.

  1. Syntax for Dereferencing a Pointer:

C

*ptr

Here:

  • ptr is a pointer variable.
  • The * is the dereference operator.

Dereferencing the pointer gives you access to the value stored at the memory location that ptr is pointing to.

  1. Example of Dereferencing a Pointer:

C

#include <stdio.h>

int main() {

    int num = 10;       // Declare an integer variable

    int *ptr = &num;    // Declare a pointer and assign it the address of 'num'

    printf("Value of num: %d\n", num);         // Output the value of 'num'

    printf("Value using pointer: %d\n", *ptr); // Dereference 'ptr' to get the value of 'num'<

    return 0;

}

Explanation:

  • num is an integer variable with a value of 10.
  • ptr is a pointer that holds the address of num (i.e., ptr = &num).
  • The dereference operator * is used to access the value stored at the memory location ptr is pointing to, which is 10 in this case.

Output:

Value of num: 10

Value using pointer: 10

  1. Dereferencing Pointers:
  • Accessing value: When you dereference a pointer, you get the value stored at the memory location the pointer is pointing to.
  • Modifying value: Dereferencing can also be used to modify the value at the memory location the pointer is pointing to.
  1. Modifying the Value Using Dereferencing:

C

#include <stdio.h>

int main() {

    int num = 5;

    int *ptr = &num;  // Pointer 'ptr' holds the address of 'num'

    // Dereference the pointer to modify the value of 'num'

    *ptr = 20; // This changes the value of 'num' to 20

    printf("New value of num: %d\n", num); // Output the new value of 'num'

    return 0;

}

Explanation:

  • Initially, num is 5.
  • The pointer ptr is assigned the address of num.
  • By dereferencing the pointer *ptr, we assign a new value (20) to num through the pointer.
  • The change is reflected in the value of num.

Output:

New value of num: 20

  1. Common Mistakes When Dereferencing Pointers:
  1. Dereferencing a NULL Pointer: If a pointer is NULL (i.e., it doesn't point to a valid memory location), dereferencing it will cause undefined behavior, often resulting in a segmentation fault.

C

int *ptr = NULL;

printf("%d", *ptr); // Dereferencing a NULL pointer - undefined behavior

Solution: Always check if the pointer is not NULL before dereferencing.

C

if (ptr != NULL) {

    printf("%d", *ptr);

} else {

    printf("Pointer is NULL\n");

}

  1. Dereferencing Uninitialized Pointers: If a pointer is not initialized to a valid memory address, dereferencing it can lead to accessing invalid memory.

C

int *ptr; // Uninitialized pointer

printf("%d", *ptr); // Dereferencing an uninitialized pointer - undefined behavior

Solution: Always initialize pointers before dereferencing.

C

int num = 10;

int *ptr = &num; // Pointer initialized to the address of 'num'

printf("%d", *ptr); // Safe dereferencing

  1. Dereferencing a Pointer After Freeing Memory: After a pointer is freed using free(), the memory is no longer valid. Dereferencing such a pointer leads to undefined behavior.

C

int *ptr = (int*)malloc(sizeof(int));

*ptr = 5;

free(ptr);

printf("%d", *ptr); // Dereferencing after freeing memory - undefined behavior

Solution: After freeing memory, set the pointer to NULL to avoid dereferencing a dangling pointer.

C

free(ptr);

ptr = NULL; // Pointer is now safe to use

  1. Key Points to Remember:
  • Dereferencing is the process of accessing the value at the address a pointer holds.
  • Use the dereference operator * to retrieve or modify the value a pointer points to.
  • Always ensure that a pointer is not NULL or uninitialized before dereferencing it.
  • After freeing dynamically allocated memory, always set the pointer to NULL to avoid dangling pointers.

Summary:

Dereferencing a pointer in C allows you to access and manipulate the value stored at the memory location the pointer is pointing to. This is done using the * operator. It is important to handle pointers carefully to avoid errors like dereferencing NULL or uninitialized pointers, and to prevent memory access violations.