C program to create, initialize and access a pointer variable

June 5, 2021

In this tutorial, we will write a C program to create, initialize and access a pointer variable. To learn the basics of pointer refer my tutorial: C Pointers

Example: Program to create, access and initialize a Pointer

In the following program we have declared a character variable ch and character pointer pCh, later we initialized the pointer variable pCh with the address value of char ch.
The example also shows how to access the value and address of ch using the pointer variable pCh

#include <stdio.h>

int main()
{
	//char variable
    char ch;

    //char pointer
    char *pCh;

    /* Initializing pointer variable with the
     * address of variable ch
     */
    pCh = &ch;

    //Assigning value to the variable ch
    ch = 'A';

    //access value and address of ch using variable ch
    printf("Value of ch: %c\n",ch);
    printf("Address of ch: %p\n",&ch);

    //access value and address of ch using pointer variable pCh
    printf("Value of ch: %c\n",*pCh);
    printf("Address of ch: %p",pCh);

   return 0;
}

admin