Question: BIT 2400 Intermediate Programming Tutorial 1b Pointers This tutorial will focus on dealing with pointers. Create a function for each problem. Create a main that

BIT 2400 Intermediate Programming Tutorial 1b Pointers This tutorial will focus on dealing with pointers. Create a function for each problem. Create a main that will call sequentially all the functions. You can simply add this code to your L01a code (if you started with that part first). Pointers Basics Create three (standard) integers (x, y and z) and initialize them with the following values, respectively: 25, 50, 73. Output the values contained in x, y and z. Create a pointer to an integer. Using this pointer, increment the value of each of the three original variables by 100. o You will need 2 steps per variable (6 steps total). Output the values contained in x, y and z.

1. Pointers Memory allocation Create a pointer to an integer Allocate a block of memory (dynamic array) that will be able to hold one integer (array size 1) Check if the memory was correctly allocated (if pointer is not null) Have your pointer point to the single element in the array. If the memory was correctly allocated, ask the user to enter an integer, and record this value using your pointer. Print out the following information (make sure to identify the information correctly in your output): o the address of the pointer variable o the content of the pointer variable o the content of the pointee (memory allocated). You should get 3 different values. Do you know why? Do you understand why the first two are hexadecimal values?

Pointers Memory deallocation Deallocate the memory of the pointer you just created. Copy the lines you had above to print out the address of the pointer, the content of the pointer and the content of the pointee, on different lines. Where is the pointer pointing to? What is the content of the pointee? You should get a run time error (or garbage values which are even worse) if you followed the instructions correctly. Your error comes from the fact that you are trying to read the value of the memory address which has been released. Comment out the line to print out the content of the pointee, and you should be all right. Assign the null value to your pointer Print the information again. Where is the pointer pointing to? What is the content of the pointee? You will also get a run time error, like before, this time because you are trying to read the value of the memory address 0 (which does not exist). Comment out the line to print out the content of the pointee, and you should be all right.

2. Pointers Arrays: allocation Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. The function should return a pointer to the array (your return value should be int *). Here is your function prototype (also called function declaration): int * arrayAllocation(int nb); In the main function, ask the user how much memory they want to allocate, i.e ask the user they for how many integers. Call the function with the users answer. Remember the return value in a pointer of integer called ptrArray.

3. Pointers Arrays: initializing content Using the pointer ptrArray, you now want to write values in those integers. Use a loop to go through each element in that array, and initialize them all to 42. This is done in the main.

4. Pointers Arrays: printing Write a function that will print the elements in the array-pointer. The function should accept a pointer to an integer, and the size of this array. The function should not return anything. Here is your function prototype: void printArray(int* ptr, int nb); Your function needs to use a loop to go through all the elements in the array, and simply prints them on a new line. In the main function, call this function with your pointer ptrArray and the size given by the user.

5. Pointers Arrays: modifying content Modify the content of the array to add 50 if the index (position in the array) is even, otherwise to subtract 50 (in the main). Print the array again using the function you created in 6. 6. Pointers Deallocation Deallocate and set your pointer to Null (in the main function)

7. Pointers to Pointers In this exercise, we will have 3 variables: a char array, a char pointer and a pointer to a char pointer. It might be good to draw things out to really understand what is pointing to what. This will make you really practice and understand the referencing (&) and dereferencing (*) operators, as well as pointer arithmetic (addition, subtraction, ++ or --). Complete each step below. Make sure you understand what happens in each of them

1. Create the following variable declaration: char *oddOrEven = "Never odd or even ";

2. Create a pointer to a char value named nthCharPtr pointing to the 6th character of oddOrEven (remember that the first item has index 0). Use the indexing operator ([]).

3. Using pointer arithmetic (addition, subtraction, ++ or --), update nthCharPtr to point to the 4th character in oddOrEven.

4. Print the address of nthCharPtr and the value currently pointed to by nthCharPtr.

5. We will now create a pointer to a pointer. They behave the same way, except that what thei pointee happens to be a pointer. If you want to draw it out, its a box that points to a second box that points to a third box that contains a char.

Create a new pointer to a pointer (a char **) named pointerPtr that points to oddOrEven.

6. Print a line that says Variable | Address | Content | Pointee. For each of these variables: pointerPtr, nthCharPtr and oddOrEven (each on a new line), print the address of the variable, value stored in the variable, and the pointee of the variable. Separate each value with a vertical bar. Do you notice anything interesting in there? Observe the following in particular The value in pointerPtr and the address of oddOrEven The address in nthCarPtr and the address of oddOrEven The value of oddOrEven and the pointee of oddOrEven

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!