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

Manual # 11: Topic

This document is a manual on pointers in C++. It contains 7 sections: 1. An introduction to pointers using basic examples of declaring a pointer variable, assigning the address of a variable to a pointer, and dereferencing the pointer. 2. More examples of pointers and addresses, showing how pointers store memory addresses. 3. An example showing how to increment and decrement the value a pointer points to. 4. An example of incrementing the pointer itself. 5. An example of a pointer pointing to an array, and how incrementing the pointer advances it through the array. 6. A more complex example using pointers to iterate through and print an array.

Uploaded by

musa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Manual # 11: Topic

This document is a manual on pointers in C++. It contains 7 sections: 1. An introduction to pointers using basic examples of declaring a pointer variable, assigning the address of a variable to a pointer, and dereferencing the pointer. 2. More examples of pointers and addresses, showing how pointers store memory addresses. 3. An example showing how to increment and decrement the value a pointer points to. 4. An example of incrementing the pointer itself. 5. An example of a pointer pointing to an array, and how incrementing the pointer advances it through the array. 6. A more complex example using pointers to iterate through and print an array.

Uploaded by

musa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Manual # 11

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];

p=j; \\\ you can also write p=&j[0];

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
------------------------------------------------------------------------------------------------------------

You might also like