#include <stdio.h>
int main()
{
int original = 5;
//Primary Pointer
int * priPointer = &original;
int ** secPointer = &priPointer;
printf("Address with Pointer: %p \n", priPointer); //Address
printf("Address without Pointer: %p \n", &original); //Address
printf("Address of Primary Pointer: %p \n", secPointer); //Address of Address
printf("------------------------------------------------ \n");
printf("Value using Variable: %d \n", original);
printf("Value using Primary Pointer: %d \n", *priPointer);
printf("Value using Secondary Pointer: %d \n", **secPointer);
return 0;
}
Result:
Address with Pointer: 000000000061FE14
Address without Pointer: 000000000061FE14
Address of Primary Pointer: 000000000061FE08
------------------------------------------------
Value using Variable: 5
Value using Primary Pointer: 5
Value using Secondary Pointer: 5
Process returned 0 (0x0) execution time : 0.022 s
Press any key to continue.
This program demonstrates the use of multiple levels of pointers in C programming.
In this program, we have defined an integer variable original
with a value of 5
. Then, we have created a primary pointer priPointer
which points to the address of original
using the &
operator.
After that, we have created a secondary pointer secPointer
which points to the address of priPointer
. This means that secPointer
contains the address of priPointer
.
We can print the addresses of original
, priPointer
, and secPointer
using the %p
format specifier in the printf()
function.
To access the value of original
through the pointers, we use the *
operator. We can print the values of original
, priPointer
, and secPointer
using the printf()
function and the *
operator.
Finally, we can access the value of original
using the secondary pointer secPointer
by dereferencing it twice with the **
operator.
Multiple levels of pointers are useful when dealing with complex data structures or when you need to pass a pointer to a pointer as an argument to a function. They allow you to modify the value of a pointer by using another pointer that points to it.
No comments:
Post a Comment