You are on page 1of 3

IT-108 Lab Practice Lab: 11 Date: 27-04-2018

Pointers

Submitted By Muhammad Adrees


15074156-101
 Why we use pointes? (5 Bullet points)
1. The use of pointers for passing alterable variables to subroutines has an
another use
2. it can save memory and run faster because it does not have to duplicate
the data.
3. Pointers are to be used where you can't use anything else. It is either
because the lack of appropriate functionality, missing data types or for
pure perfomance.
4. We use pointers to store memory addresses.
5. Pointers allow you to refer to the same space in memory from multiple
locations. This means that you can update memory in one location and
the change can be seen from another location in your program. You will
also save space by being able to share components in your data
structures
6. You should use pointers any place where you need to obtain and pass
around the address to a specific spot in memory. You can also use
pointers to navigate arrays
 Write a program that will accept five values from user. The values should store in an array by
using pointer. Print the array element using loop and also without loop. Also print the
elements in reverse order.
// Lab11.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
int enter[5];
int *p;
cout << "Enter Five values" << endl;
cin >> enter[0];
cin >> enter[1];
cin >> enter[2];
cin >> enter[3];
cin >> enter[4];
p = enter;
cout << "Values are printing through loop" << endl;
for (int i = 0; i < 5; i++) {
cout << "\n" << *(p + i) << endl;
}
cout << "Values are printing without loop" << endl;
cout << *(p + 0) << endl;
cout << *(p + 1) << endl;
cout << *(p + 2) << endl;
cout << *(p + 3) << endl;
cout << *(p + 4) << endl;
cout << "Values are printing with reverse order in loop" << endl;
for (int i = 5; i > 0; i--) {
cout << *(p + i) << endl;
}

system("pause");
return 0;
}
 Assume the variable declarations:
Int Foo = 0;
Int *ptr = &Foo;
Which of the following statements will change the value of Foo to 1?
1) ptr++;
2) Foo++;
3) (*Foo)++;
4) (*ptr)++;
5) All of these
6) 1 and 2 only
7) 1 and 4 only
8) 2 and 4 only
9) 3 and 4 only
10) None of these
ANSWER
Only 2
Foo++;
 Write a program that will enter two integers through pointer. Pass these values of function
and swap them.
// ConsoleApplication12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;
void swap(int *a, int *b) {
int temp = 0;
temp=*a;
*a = *b;
*b = temp;
cout << "The swap value of a = " << *a << endl;
cout << "The swap value of b = " << *b << endl;
}
int main()
{
int p, q;
cout << "Enter two values a and b for swapping" << endl;
cin >> p;
cin >> q;
swap(&p, &q);
system("pause");
return 0;
}

 Enter string data in array. Pass this array to pointers and search specific character into string.
E.g Enter Computer, search P in computer string.
 Write a class that will take some data through member functions. Differentiate between
POINTERS TO OBJECTS and ARRAY OF POINTERS TO OBJECTS. Use New and Delete operator
into your program.

You might also like