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
/* Created by Chaitanya for Beginnersbook.com * C program to create, initialize and access a pointer */ #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; }
Output:
Related C Examples
1. C program to check leap year
2. C program to swap two numbers
3. C program to find the size of int, float, double and char
4. C program to find ASCII value of a character
Leave a Reply