Access Pointers in C programming.
In C Programming Language, pointers are variables that store memory addresses of another. we can access a pointer directly dereferencing it using the * (asterisk) Operator. This allows programmer to retrieve the value stored at the memory address pointed to by the pointer.
There are basically two ways in which a program can access a variable content and then manipulate it: Direct Access and Indirect Aceess.
Accessing Pointers- Directly and Indirectly
1. Access pointer Directly:
In C programming language, the definition of direct access refers to a pointer that points directly to a memory location. This means that a pointer holds the memory address of a variable or data structure, thereby directly accessing the data stored there.
With the help of a direct access pointer, we can easily write to or read from the memory location pointed to by the pointer without additional instructions or steps. This can be useful for accessing and modifying data in memory in C programs.
An example to illustrate a direct access pointer in C Language:
#include <stdio.h>
int main()
{
int n = 100; // Declare and initialize variable num
int *ptr; // pointer variable Declaration
ptr = &n; // Assign the address of 'n' to the pointer variable'ptr'
printf("Value of n: %d\n", n);
printf("Value of n using pointer: %d\n", *ptr);
*ptr = 20; // Update the value of 'n' through the pointer
printf("Updated value of num: %d\n", n);
return 0;
}
In this example, we declare an integer variable `n` with a value of 100. Then we declare the pointer variable `ptr` and assign the memory address of `n` using the address operator `&`. This allows `ptr` to directly access and manipulate the value of `n`. We can then use the pointer to read the value of `n` and update it without accessing it directly.
2. Access Pointer indirectly.
In C programming Language, direct access point refers to a pointer that points to a pointer, which points to the actual data. This allows multiple levels of manipulation, where direct pointers can be used to access or modify data values indirectly through intermediate pointers.
Example to illustrate a indirect access pointer in C Language:
#include <stdio.h>
int main()
{
int n = 100;
int *ptr = &n;
int **indPtr = &ptr;
printf("Value of num: %d\n", **indPtr);
return 0;
}
In this example, `indPtr` is an indirect pointer that points to the `ptr` pointer, which points to the integer parameter `n'. By dereferencing 'indPtr' twice ('**indPtr'), we can access the value of 'n' indirectly through the intermediate 'ptr'.
Diffrence between Directly and indirectly accessin pointers
Direct Access Pointers | Indirect Access Pointers |
Point directly to a specific memory location. | Store the memory address of another variable. |
Can directly access and modify data at the memory location without additional steps. |
Need to dereference the pointer to access or modify the data indirectly. |
Uses the actual memory address in the pointer. | Uses the memory address of another variable in the pointer |
int *ptr = &variable; *ptr = 10; | int *ptr; ptr = &variable; *ptr = 10;
|