facebook like button

01 June, 2011

program 50: using pointers in C

The need: 
      This program was written by me to test the use of pointers in C program. In this program I just declare a pointer, initialized it and used that pointer in some operations.

The code: 
--------------------------------------------
#include<stdio.h>
main()
{
  int x,y;
  int *p,*q;
  p=&x;        //p is having address of x
  q=&y;        //q is having address of y
  printf("Enter the values of x and y\n");
  printf("x=");
  scanf("%d",&x);
  printf("y=");
  scanf("%d",&y);
  printf("\nvalue of x is %d\n",*p);
  printf("address of x is %d\n",&x);
  printf("value of p is %d\n\n",p);
  printf("value of y is %d\n",*q);
  printf("address of y is %d\n",&y);
  printf("value of q is %d\n\n",q);
  return 0;
}
--------------------------------------------

The approach: 
Copy, paste, compile and run the program. This is a simple program just to show how pointers can be used in a program. Like pointers p and q are initialized to addresses of variables x and y using &(address operator). This only verifies the concept of address and pointers.
After that these can be used normal variables.
 

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply