Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

clarify and articulate your research methodology;

Answered: 1 week ago

Question

consider how to build on prior learning.

Answered: 1 week ago