Professional Documents
Culture Documents
Topic:
Pointers
Subject:
Introduction to Computing
Author:
Engr. Ali Faisal Murtaza
1
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
{
int *p;
int x;
x=10;
cout<<x<<endl;
p=&x;
cout<<*p;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
{
int *p;
int x;
p=&x;
cout<<&x<<endl;
cout<<p<<endl;
x=10;
cout<<x<<endl;
cout<<*p;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
2
{
int *p,x;
p=&x;
*p=100;
(*p)++;
cout<<x<<endl;
(*p)--;
cout<<x<<endl;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
{
int *p,x;
p=&x;
*p=100;
cout<<p<<endl;
*p++;
cout<<p<<endl;
cout<<x<<endl;
(*p)--;
cout<<x<<endl;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
3
{
int *p, j[10];
cout<<&j[0]<<endl;
cout<<p<<endl;
p++;
cout<<&j[1]<<endl;
cout<<p<<endl;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
{
int *p[10],i[10],y;
for(y=0;y<10;y++)
{
p[y]=&i[y];
}
for(y=0;y<10;y++)
{
cout<<*p[y]<<” “<<p[y]<<endl;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
int main()
{
int *p[10],i[10],y;
for(y=0;y<10;y++)
4
{
i[y]=y;
}
for(y=0;y<10;y++)
{
p[y]=&i[y];
}
for(y=0;y<10;y++)
{
cout<<*p[y]<<endl;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Q1: Introduce int variables x and y and int* pointer variables p and q. Set x to 2, y to 8,
p to the address of x, and q to the address of y. Then print the following information:
(1) The address of x and the value of x.
(2) The value of p and the value of *p.
(3) The address of y and the value of y.
(4) The value of q and the value of *q.
(5) The address of p (not its contents!).
(6) The address of q (not its contents!).
------------------------------------------------------------------------------------------------------------
Q2: Introduce an array of five elements int a[5], and int based pointer variable p. And
using this pointer ‘p’ enter values: 32, 22, 3, 9 & 6 respectively in an array.
------------------------------------------------------------------------------------------------------------
Q3: Write a program which will take ten values from the user. And the program should
display the average of those ten values. Should be done through pointers.
Hint: Arrays and Pointers can be used.
------------------------------------------------------------------------------------------------------------
Q4: Input and Display the 3 x 3 matrix through pointers using 2D pointer
------------------------------------------------------------------------------------------------------------