Question

[C++ Language] Look at the following code and comment each line about how it works with...

  1. [C++ Language] Look at the following code and comment each line about how it works with pointer.

int i = 33;

double d = 12.88;

int * iPtr = &i;   

double * dPtr = &d; //

iPtr = &d;   //

dPtr = &i;   //

iPtr = i;    //

int j = 99;

iPtr = &j; //

0 0
Add a comment Improve this question Transcribed image text
Answer #1

check out the solution.

----------------------------------------------------

#include <iostream>

using namespace std;

int main() {
int i = 33; // declare an int variable and assign 33 to it
double d = 12.88; // declare a double variabe and assign 12.88 to it
int * iPtr = &i; // save address of int variable 'i' in int type pointer iPtr that is int type pointer iPtr now points to 'i' address
double * dPtr = &d; // save address of double variable 'd' in double type pointer dPtr that is double type pointer dPtr now points to 'd' address
iPtr = &d; // save memory address of 'd' in pointer iPtr - this throws error - as 'd' is double type and iPtr is of int type so compilation error
dPtr = &i; // save memory address of 'i' in pointer dPtr - this throws error - as 'i' is int type and dPtr is of double type so compilation error
iPtr = i; // save value of 'i' in pointer iPtr - this throws error - as 'i' is int type and iPtr is of int type pointer - so compilation error
int j = 99; // declare an int variable and assign 99 to it
iPtr = &j; // save address of 'j' to iPtr which both are of int type
}

-----------------------------------------------------------

Errors after compilation :

jdoodle.cpp: In function int main(): jdoodle.cpp:10:12: error: cannot convert double*) to int*) in assignment 10 iPtr = &d

----------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
[C++ Language] Look at the following code and comment each line about how it works with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Look at the following code and comment each line about how it works with pointer. int...

    Look at the following code and comment each line about how it works with pointer. int i = 33; double d = 12.88; int * iPtr = &i;    double * dPtr = &d; // iPtr = &d;   // dPtr = &i;   // iPtr = i;    // int j = 99; iPtr = &j; //

  • This is in java language. Please use simple java programming language. Each of the parts a....

    This is in java language. Please use simple java programming language. Each of the parts a. through c. below is preceded by a comment indicating what the code do. There is a least one problem with each section of code and it fails to do what was inte Show how to modify or rewrite the code so that it works as intended. Assume that all varia used have already been declared. а. // INTENT : given an array arr of...

  • Question 2 (a) Provide a single line of C++ code that will declare a variable that...

    Question 2 (a) Provide a single line of C++ code that will declare a variable that points to a string value, the variable is named x. [2 marks] (b) You have array c declared as string *c [5]; Give the single line of C+ + code that will assign the string pointer stored in variable x (from above) to the third element of the array c. [2 marks] (c) Write a for loop that initialises the contents of each element...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

  • Python 3.7.2 for language Please comment for each line of code good naming is important 1....

    Python 3.7.2 for language Please comment for each line of code good naming is important 1. Answer the question: what is the Python regular expression pattern that would match a hex color (https://en.wikipedia.org/wiki/Web_colors ) (for example, the pattern that would match an email address is '[\w.-]+@[\w.-]') Write an algorithm for step 3. As part of your algorithm, be sure to describe the pattern you're using to find the win/loss result for each game. Write a program that looks at the...

  • Given the following Java code: class C { public int foo(C p) { return 1; }...

    Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...

  • Use only C++ for all function Introduce a call-by-value function that computes the volume of a...

    Use only C++ for all function Introduce a call-by-value function that computes the volume of a box. Hint: Length, width, and height of a box is needed. Introduce a call-by-reference function with void output. that computes the square of an integer. Please double check the value of the function input before and after function call. Introduce a call-by-pointer function with void output. that computes the square of an integer. Please double check the value of the function input before and...

  • in C language please! 9. 14 points Show what the following C code will print. const...

    in C language please! 9. 14 points Show what the following C code will print. const int MAX_I_COUNT = 4; const int MAX_J_COUNT = 15; int i, j; for( i = 1; i <MAX_I_COUNT; i++) { for(j = 2; j <MAX_J_COUNT; j += 4 ) printf("%d", j + i); puts("\n-----" ); puts( "I'm Done!" ); 10. 14 points What would be printed? int bean = 5, cheese = 3; int *p = &bean; *p += bean; ++cheese; *p += cheese;...

  • URGENT HELP NEEDED :( Convert the following C++ program into an x86 assembly language program. Comment...

    URGENT HELP NEEDED :( Convert the following C++ program into an x86 assembly language program. Comment the start of each "code block" that performs one of the listed mathematical calculations. Comments go to the right of the actual code, all starting on the same column. Post ONLY your ASM file here to Blackboard when complete. // Global variables char a = 5; short b = 7; int   c = 11; int   d = 13; // Code int main() {    ...

  • The following lines of code all have problems. Identify what is wrong with each line of...

    The following lines of code all have problems. Identify what is wrong with each line of code The following lines of code all have problems. Identify what is wrong with each line of code. a) for(j=0; j<= 10; j++) cout << prices[j]; b) int array = {1,2,3,4}; c) int arr[3]; for (arr = 0; arr < = 10; arr++) d) char k; for (k=0; k<= 10; k++)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT