Professional Documents
Culture Documents
Pointers
#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.