Docsity

Computer programming lab, Lab Reports of Computer Programming

Toggle sidebar
bg1
Lab 04: Pointers
Objectives
To learn the usage of pointers
To differentiate between &ptr, ptr and *ptr
To learn basic DMA for singular variables and the concept of free store
Pointers
Declaring a pointer
Syntax: dataType *pointerName;
Eg: Declare an integer pointer called iPtr
int *iPtr;
Declaring and assigning a pointer to a variable
Syntax: dataType *pointerName = &variableName;
Eg: Declare an integer pointer called iPtr and point it to the variable sum
int *iPtr = ∑
Pointer manipulation
Eg:
int sum=77;
int *iPtr = ∑
cout << "Addr of sum: " << &sum << endl;
cout << "Addr of iPtr: " << &iPtr << endl;
cout << "Value of iPtr: " << iPtr << endl;
cout << "Value of variable pointed by iPtr: " << *iPtr << endl;
Observe the difference in the iPtr used:
- & for the address of where the iPtr pointer is kept in memory
- pointer name for the value that is being kept by the iPtr pointer. Naturally will be the address of
the variable that iPtr is pointing to
- * for dereferencing the iPtr pointer to goto the variable it’s pointing to and retrieve the value
DMA
Uses the keywords
- new to allocate a space in the memory to store data
- delete to release the memory space held
Creating a single dynamic variable
Syntax: dataType *varName = new dataType;
Eg: dynamically create an integer variable called tmp
int *tmp = new int;
pf3
pf4

Partial preview of the text

Download Computer programming lab and more Lab Reports Computer Programming in PDF only on Docsity!

Lab 04: Pointers

Objectives

 To learn the usage of pointers

 To differentiate between &ptr, ptr and *ptr

 To learn basic DMA for singular variables and the concept of free store

Pointers

Declaring a pointer

Syntax : dataType *pointerName;

Eg: Declare an integer pointer called iPtr

int *iPtr;

Declaring and assigning a pointer to a variable

Syntax : dataType *pointerName = &variableName;

Eg: Declare an integer pointer called iPtr and point it to the variable sum

int *iPtr = ∑

Pointer manipulation

Eg:

int sum=77;

int *iPtr = ∑

cout << "Addr of sum: " << &sum << endl;

cout << "Addr of iPtr: " << &iPtr << endl;

cout << "Value of iPtr: " << iPtr << endl;

cout << "Value of variable pointed by iPtr: " << *iPtr << endl;

Observe the difference in the iPtr used:

  • & for the address of where the iPtr pointer is kept in memory
  • pointer name for the value that is being kept by the iPtr pointer. Naturally will be the address of

the variable that iPtr is pointing to

  • ***** for dereferencing the iPtr pointer to goto the variable it’s pointing to and retrieve the value DMA

Uses the keywords

  • new to allocate a space in the memory to store data
  • delete to release the memory space held

Creating a single dynamic variable

Syntax : dataType *varName = new dataType;

Eg: dynamically create an integer variable called tmp

int *tmp = new int;

Question 1

1.1. The following program shows a simple pointer and variable assignment.

#include using namespace std; int main() { int a; int *aPtr; aPtr = &a; cout << "Enter an integer: "; cin >> *aPtr; // input: 75 cout << "Address of a is " << &a << endl; cout << "Address of aPtr is " << &aPtr << endl; cout << "aPtr is pointing to " << aPtr << endl; cout << "Value of a is " << a << endl; cout << "Value of *aPtr is " << *aPtr << endl; return 0; }

Answer the following questions without compiling and running the program:

a) What are the outputs of the above program?

b) What is the difference between printing aPtr, &aPtr and *aPtr?

c) What is the relationship between a and *aPtr?

1.2. This program shows that operations can be done on dereferenced pointer variables.

#include using namespace std; int main() { int a = 3, b; int *aPtr = &a, *bPtr = &b; *bPtr = *aPtr + 900; cout << "Value of b is " << b << "\n"; // output A cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output B bPtr = (aPtr)++; cout << "Value of b is " << b << "\n"; // output C cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output D bPtr = ++aPtr; cout << "Value of b is " << b << "\n"; // output E cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output F return 0; }

Answer the following questions without compiling and running the program:

a) What are the outputs of A, B, C, D, E and F?

b) Explain how do you get the outputs?

Question 2

Question 3

Try to use the concept of pointers and complete the below C++ program for selection sort.

#include using namespace std; int main() { const int MAX = 10; int array[MAX] = {8, 6, 10, 2, 16, 4, 18, 14, 12, 20}; /smallest pointer is used to point to the smallest element in the unsorted list, and, current pointer is used to point to the current position to be sorted/** int *smallest, *current = array; for(int i=0; i<MAX; i++, current++){ smallest = current; cout << "\nCurrent=" << current; //search for smallest element in the unsorted list // WRITE YOUR CODE cout << " Smallest=" <<smallest<< endl; //swap smallest and current // WRITE YOUR CODE //display the modified array after swap // WRITE YOUR CODE } //display the final sorted array // WRITE YOUR CODE return 0; }