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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started