Home /
Expert Answers /
Computer Science /
a-pointer-is-a-variable-that-stores-the-memory-address-of-an-object-pointers-are-used-extensively-pa689
(Solved):
A pointer is a variable that stores the memory address of an object. Pointers are used extensively ...
A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C anc 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).
Figure 1: Program Segment 1 i. Draw a memory diagram to show the content of pointer(s) and variable(s) after the program segment executed.
i. Draw a memory diagram to show the content of pointer(s) and variable(s) after the program segment is executed. (4 marks) ii. Write the output of the program segment. (9 marks) (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. (9 marks) (c) State TWO (2) advantages of using pointers in a program. (3 marks)