0% found this document useful (0 votes)
19 views10 pages

CNLAB08

Uploaded by

Atta Ur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

CNLAB08

Uploaded by

Atta Ur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab # 8

Computer Networks

Course Instructor Ms. Momina Azam

Abeera Ashaf
Lab Instructor(s)
Noor Tayyaba Batool
Semester Spring 2024

Department of Computer Science ITU, Lahore


Pakistan
Read the following instructions carefully:
All tasks mentioned in the lab manual are individual tasks. Copyingfrom anyone is
not allowed at all. Make sure you do not borrow answers from anyone.

Section 1: Dry run the programs below for understanding Pointers

1. What will be the output of the following program?

int main()
{
int f = 32, *ptr = &f;//pointer is pointing towards address of variable 'f'//

char mo = 'A', & moo = mo;//Aliasing


moo += f;
*ptr += moo;
cout << f << "," << mo << endl;

return 0;
}

Output: 129,a

Moo acts as an alias for mo, when moo is incremented by value of ‘f’, the value of mo
automatically updates to 129
2. What will be the output of the following program?

3. What will be the output of the following program?

int main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
return 0;

Explanation:
Initially, a pointer points towards first index address of an array by default, when we perform pointer
arithmetic, pointer is now pointing towards second element, that’s why value of *p is stored as 5

When we cout<<*arr+10, it adds ten to the first element of the array and prints it.
4. What will be the output of the above code snippet?
Explain how pointer arithmetic is used to access array elements.
What is the purpose of the delete keyword in the below code?
What happens if you forget to delete a dynamically allocated object?

// Additional example: Deleting dynamically allocated object


MyClass* ptr_class2 = new MyClass();
ptr_class2->x = 50;
ptr_class2->y = 6.78;
ptr_class2->display(); 6.
delete ptr_class2; 8.

7.
int & r = n;
int* p = &n;

int* q = p;

8.
Write a function that prints every address of an integer array passed in as a parameter.
The
size of the array is also passed to the function. 2

Code:
void print_add(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
cout << &arr[i]<<" ";
}
}
int main()
{
int size = 5;
int array[5] = { 1,2,3,4,5 };
print_add(array, size);
return 0;
}

Output:
Section 2: Lab Implementations:
Part 1: Pointers to Primitive Types
1. Declare a pointer variable for each primitive
type (int as ptr_int ,
float as ptr_float,
char as ptr_char;private static int MAX = 5,

double as ptr_double)
and print their sizes.
2. Assign values to these variables given below.
3. Dereference each pointer and print the values.

Code:
int main()
{
int a = 5;
double b = 3.6;
float c = 6.6;
char d = 'A';

int* ptr_int;
double* ptr_double;
char* ptr_char;
float* ptr_float;

cout << "size of float pointer: " << sizeof(ptr_float) << endl;
cout << "size of double pointer: " << sizeof(ptr_double) << endl;
cout << "size of int pointer: " << sizeof(ptr_int) << endl;
cout << "size of character pointer: " << sizeof(ptr_char) << endl;

ptr_int = &a;
ptr_double = &b;
ptr_float = &c;
ptr_char = &d;

cout << "Integer: " << *(ptr_int) << endl;


cout << "Double: " << *(ptr_double) << endl;
cout << "Character: " << *(ptr_char) << endl;
cout << "Float: " << *(ptr_float) << endl;

return 0;
}
Part 2: Pointers to Class Objects
1. Define a simple class as MyClass with some member variables and methods.
2. Create a pointer to an object of this class using new.
3. Dereference the pointer and access individual members of the class object.
4. Print the size of the pointer.

Code:
class MyClass
{
public:
int a;
int b;
int c;
int sum;

int Summation()
{
sum = a + b + c;
return sum;
}
MyClass(int x, int y, int z)
{
a = x;
b = y;
c = z;
}

};

int main()
{

MyClass* ptr = new MyClass(2,5,8);


cout << "Sum: " << ptr->Summation() << endl;

cout << "A: " << ptr->a << endl;


cout << "B: " << ptr->b << endl;
cout << "C: " << ptr->c << endl;

return 0;
}

Part 3: Void Pointers


1. Declare a void pointer.
2. Assign addresses of different types of variables to the void pointer.
3. Dereference the void pointer and perform type casting to access the original values.
4. Print the size of the void pointer.
Code:
int main()
{
int a = 5;
double b = 3.6;
float c = 6.6;
char d = 'A';

void* ptr = NULL;


ptr = &a;
cout << *((int*)ptr) << endl;
ptr = &b;
cout << *((double*)ptr) << endl;
ptr = &c;
cout << *((float*)ptr) << endl;
ptr = &d;
cout << *((char*)ptr) << endl;

cout << "Size:" << sizeof(ptr);

return 0;
}

Output:

You might also like