Google News
logo
C program to Create, Initialize and Access a Pointer Variable
In the following example of a C program that creates, initializes, and accesses a pointer variable :
Program :
#include <stdio.h>

int main() {
    int num = 10;
    int *ptr;

    ptr = &num;

    printf("The value of num is %d\n", num);
    printf("The value of ptr is %p\n", ptr);
    printf("The value pointed to by ptr is %d\n", *ptr);

    return 0;
}
Output :
The value of num is 10
The value of ptr is 0x7fff88d4824c
The value pointed to by ptr is 10
In this example, the program creates an integer variable num and initializes it with the value 10. The program then creates an integer pointer variable ptr without initializing it.

Next, the program initializes ptr to point to the memory address of num using the address-of operator &.

Finally, the program uses the printf function to print the value of num, the value of ptr, and the value pointed to by ptr.

The output of this program would be vbnet :
The value of num is 10
The value of ptr is 0x7ffd1aa0d82c
The value pointed to by ptr is 10​

 

This program demonstrates how to create, initialize, and access a pointer variable in C. You can modify this program to create and access pointers to other data types, or to perform pointer arithmetic and dynamic memory allocation.