Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C Program 1: Warmup Exercises with Pointers Sometimes when we're working with a new subject, like pointers, it helps to do a series of

In C

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Program 1: Warmup Exercises with Pointers Sometimes when we're working with a new subject, like pointers, it helps to do a series of smaller problems before we tackle a bigger one. That is the purpose of this first program For this problem, write a single program that does the following 12 tasks. Please put clear comments in your code to indicate when the various tasks are being done. 1) Create two int variables x and y and initialize them to 5 and 10 respectively. 2)Create two int pointers, ptrl and ptr2, and initialize them to point to x and y respectively. 3)Verify that x and y were initialized properly by printing out their values and their addresses (i.e., using the address operator on x and y) as follows: Stage 1: x -XXX, (Note that we've designed the output so that the values are on the left and the addresses are on the right. This makes it easier to align the values.) 4)Verify that ptrl and ptr2 were initialized properly by printing the variables they are pointing to on the left and their addresses on the right as follows. In this step, print out all 4 items using the pointers only: Stage 2: *ptr1 = XXX ; *prt2-XXX ; ptr1 = XXX, ptr2-XXX (Note: even though we are using pointers here, these output values should be the same as in Stage 1.) 5) Usingptr1 and ptr2 only, change x to 10 and y to 15. (Note: do not access x and y directly.) 6)Verify that x and y were changed as follows: Stage 3: x = XXX, y = XXX; &x = XXX ; &y = XXX 7) Write a swapInt ) function that changes the values of x and y. Call it on x and y. (It is important that you use this exact name for this function. Do not use the name swap ).) Verify that x and y were changed as follows: Stage 4: x = XXX, y = XXX ; &x = XXX ; &y = XXX (Here the values for x and y should be the reverse of State 3, but the addresses will remain the 8) same.) 9Write a swapIntPtr function that changes the values ofptrl and ptr2. Call it on ptrl and ptr2. (Again, it is important to use this exact name for this function. Note that there is no function overloading in C (therefore, this function must be named differently than the function referenced above).) 10) Verify that ptrl and ptr2 were changed properly as follows: Stage 5: *ptr1 = XXX ; *prt2 = XXX ; ptr1 = XXX, ptr2 = XXX Now the address contained in ptrl should be the same address as &y, and the address contained in ptr2 should be the same address as &%) Change the variable pointed to by ptrl to 77, and change the variable pointed to by ptr2 to 88. 11) 12) Verify that x and y were changed as follows: Output Requirements These types of experiments are easier to interpret if the output is neat and well organized. Here is an example of the output from a correct run of this program. Yours should look just like this (but with different addresses, of course) Stage 1: Stage 2: *ptrl-5, *ptr2-10; ptrl - 0032FD10, Stage 3: Stage 4: Stage 5: *ptrl10, *ptr215; ptrl0032FD04, ptr2 0032FD10 Stage 6: = 10; ptr2 - 0032FDO4 x = 10 x15 y = 15; y-10 & 0032FD04 x = 88 y- 77; Of course, the addresses will be different for each run of the program. Nevertheless, the stages are clearly marked and all of the integers and all of the addresses line up. As a result, we can now see exactly what is happening. Stage 3 occurs after the values for x and y were changed using ptrl and ptr2. Stage 4 occurs after the swapInt () function is called on x and v. And Stage 5 occurs after the swapIntPtr () function is called on ptrl and ptr2. As we can see in the last line (i.e.. Stage 5), ptr1 now points to v and ptr2 now points to x. Please make your output neat and aligned as demonstrated above. All of the integer values are on the left, all of the addresses are on the right. Each value should be clearly identified and everything should line up neatly. Final Notes Errors and Warnings As we grade this assignment, we are going to look in particular for errors or warnings of the following types: warning C4047:'function' 'int *" differs in levels of indirection from int ** or warning C4024: 'swapIntPtr' : different types for formal and actual parameter 1 These particular warnings were generated in Visual Studio, and so, of course, they may be slightly different with different compilers. Nevertheless, these types of warnings often occur when we have a data type mismatch involving pointers. For example, they can occur when we attempt to send an int into a function that is expecting an "int *". Note that in this case, the levels of indirection are different. Most compilers will correctly identify that

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

Step: 3

blur-text-image

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

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Describe factors that influence training and development.

Answered: 1 week ago

Question

Identify some training issues in the global context.

Answered: 1 week ago