Professional Documents
Culture Documents
C++ Lab 12 Alpha
C++ Lab 12 Alpha
C++ Lab 12 Alpha
Pointers
This lab has been designed to enable students to practice creating and using pointers. In this
lab students will practice how to create and use pointer variables, how to pass pointers as
arguments to functions, how to use array as pointers and creating dynamic arrays. Clear
concept about all these things has already been provided in the class along with examples
and is also mentioned in lab handouts.
Objectives:
In this lab, students will practice:
• Declare and use pointer variables.
• Using pointer as arrays.
• Creating dynamic arrays.
plus the null terminator. It should copy the sentence to the dynamically allocated array,
and then return a pointer to the array. Demonstrate the function in a complete program.
Question # 4: 2/
Write a function that accepts an int array and the array’s size as arguments. The function
should create a new array that is one element larger than the argument array. The first
element of the new array should be set to 0. Element 0 of the argument array should be
copied to element 1 of the new array, element 1 of the argument array should be copied
to element 2 of the new array, and so forth. The function should return a pointer to the
new array.
Question # 5: 2/
Write a program that dynamically allocates memory for an integer array using the new
operator. The user is then asked to enter the values for the array elements. After displaying
the original array elements, the user is prompted to enter an index to update a specific
element in the array. If the index is valid, the program asks for a new value and updates
the array accordingly. Finally, it displays the updated array elements.
Question # 6: 5/
Do as directed:
int x = 50, y = 60, z = 70; Output
int *ptr;
cout << x << " " << y << " " << z << endl;
ptr = &x;
*ptr *= 10;
ptr = &y;
*ptr *= 5;
ptr = &z;
*ptr *= 2;
cout << x << " " << y << " " << z << endl;
for (int x = 0; x < 100; x++) Rewrite the following loop so it uses
cout << arr[x] << endl;
pointer notation (with the indirection
operator) instead of subscript
notation.
****************