0% found this document useful (0 votes)
54 views

Pointer Programs

This document contains C code examples that demonstrate the use of pointers to access values stored in memory locations. The examples show how to declare and initialize pointer variables, print the addresses and values of variables being pointed to, perform arithmetic operations on pointers, and use pointers to traverse one-dimensional and two-dimensional arrays. The output from each example is included and verifies that the pointers are correctly used to access the intended memory locations and values.

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Pointer Programs

This document contains C code examples that demonstrate the use of pointers to access values stored in memory locations. The examples show how to declare and initialize pointer variables, print the addresses and values of variables being pointed to, perform arithmetic operations on pointers, and use pointers to traverse one-dimensional and two-dimensional arrays. The output from each example is included and verifies that the pointers are correctly used to access the intended memory locations and values.

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* use of pointers*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
int *p,*q;
clrscr();
p=&a;
q=&b;
printf("Address of a is %d and its value is %d\n",p,*p);
printf("Address of b is %d and its value is %d\n",q,*q);
getch();
}

Output:

Address of a is -12 and its value is 10


Address of b is -14 and its value is 20

/* Evaluation of pointer expressions */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
clrscr();
a=12 ;
b=4 ;
p1=&a ;
p2=&b ;
x=*p1 * *p2 - 6 ;
y= 4 * - *p2 / *p1 +10 ;
printf( “Address of a = %d\n”,p1);
printf( “Address of b = %d\n”,p2);
printf(“a=%d, b=%d\n”,a,b);
printf(“x=%d, y=%d\n”,x,y);
*p2 = *p2 + 3;
*p1 = *p2-5;
z = *p1 * *p2 -6;
printf(“a=%d, b=%d\n”,a,b);
printf(“z=%d\n”,z);
}
Output:

Address of a = -108
Address of b = -110
a=12, b=4
x=42, y=9
a=2, b=7
z=8

/* pointers to one dimensional arrays */

#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]={1,2,3,4,5};
int *p=x,i,j,n;
clrscr();
for(i=0;i<5;i++)
{
printf("Address of x[%d] is %d and its value is %d\n",i,p,*p);
p++;
}
getch();
}

Output:
Address of x[0] is -20 and its value is 1
Address of x[1] is -18 and its value is 2
Address of x[2] is -16 and its value is 3
Address of x[3] is -14 and its value is 4
Address of x[4] is -12 and its value is 5

/* pointers to two dimensional arrays */

#include<stdio.h>
#include<conio.h>
void main()
{
int x[3][4]={1,2,3,4,5,6,4,5,6,7,8,9};
int *p=x,i,j,n;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{
printf("Address of x[%d][%d] is %d and its value is %d\n",i,j,p,*p);
p++;
}
getch();
}

Output:

Address of x[0][0] is -36 and its value is 1


Address of x[0][1] is -34 and its value is 2
Address of x[0][2] is -32 and its value is 3
Address of x[0][3] is -30 and its value is 4
Address of x[1][0] is -28 and its value is 5
Address of x[1][1] is -26 and its value is 6
Address of x[1][2] is -24 and its value is 4
Address of x[1][3] is -22 and its value is 5
Address of x[2][0] is -20 and its value is 6
Address of x[2][1] is -18 and its value is 7
Address of x[2][2] is -16 and its value is 8
Address of x[2][3] is -14 and its value is 9

You might also like