Question: A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three main

A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, to pass functions and to iterate over elements in arrays or other data structures.

(a) Consider the following program segment shown in Figure 1 and assume that we are using the usual node definition (with data member called data and link to the next node called link).

i. Draw a memory diagram to show the content of pointer(s) and variable(s) after the program segment is executed.

ii. Write the output of the program segment.

(b) Write the C++ code to perform the following:

Initializes integer type variables num1 and num2 to the values 123 and 456. Declare pointers ptr1 and ptr2 of same type with num1 and num2. Initialize ptr1 to point to num1 and ptr2 to point to num2. Swap the values of num1 and num2 values using direct access and using indirect access. Then print the values indirectly accessed by the swapped pointer.

(c) State TWO (2) advantages of using pointers in a program.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!