Wednesday 20 August 2014

Filled Under: ,

Example program for pointer in C

Share
Here is an example program for pointer in c is given below.Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let’s start learning them in simple and easy steps.C Pointer is a variable that stores/points the address of another variable. As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which will print the address of the variables defined.C Pointer is used to allocate memory dynamically i.e. at run time. The variable might be any of the data type such as int, float, char, double, short etc.

 Example program for pointer in C

#include <stdio.h>
int main()
 {
 int *ptr, q;
 q = 50;
 /* address of q is assigned to ptr */
 ptr = &q;
 /* display q's value using ptr variable */
 printf("%d", *ptr);
 return 0;
 }

Output:
50