Question

Explain what each line of the following snippet of code does: int *ptr; ptr = new...

Explain what each line of the following snippet of code does:

int *ptr;
ptr = new int;
*ptr = 10;
cout << ptr << ' ' << &ptr << ' ' << *ptr;
delete ptr;
cout << ptr << ' ' << &ptr << ' ' << *ptr;

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//declaring a pointer
int *ptr;

//assigning memory for the pointer variable
ptr = new int;

//Assigning value to the memory address sored in the ptr variable
*ptr = 10;

//printing the value of ptr
//then printing the address of ptr
//then printing the value of address stored in ptr
cout << ptr << ' ' << &ptr << ' ' << *ptr;

//delete ptr
delete ptr;

//printing the value of ptr
//then printing the address of ptr
//then printing the value of address stored in ptr
cout << ptr << ' ' << &ptr << ' ' << *ptr;
Add a comment
Know the answer?
Add Answer to:
Explain what each line of the following snippet of code does: int *ptr; ptr = new...
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
  • Consider the following code snippet. What will be printed by *ptr and ptr after the lines...

    Consider the following code snippet. What will be printed by *ptr and ptr after the lines shown. Explain with screenshots. int a[4] = { 8, 3, 5, 6}; int *ptr = a; ptr ++;

  • 51. What is the output of the following code snippet? int number = 0; int ptr_num...

    51. What is the output of the following code snippet? int number = 0; int ptr_num -&number ptr_num 60; number-80 cout < "ptr num << endl b, 60 c. 80 d. the address of number Answer 52. What is the output of the following code snippet? double num-0.0; double* ptr = &num; num = 15.0; ptr ptr 15.0 cout << num <<"ptr <<endl; a. 15 15 b. 15 30 С. 30 15 d. 30 30 Answer: 53. What is the...

  • c++ only. Please follow directions. What does the following program print and why? Comment each line...

    c++ only. Please follow directions. What does the following program print and why? Comment each line of code to explain what it is doing.   #include <iostream> using namespace std; int main() { int track[ ] = { 10, 20, 30, 40 }; int * ptr; ptr = track; track[1] += 30; cout << * ptr << " "; *ptr -= 10; ptr++; cout << * ptr << " "; ptr += 2; cout << * ptr << " "; cout...

  • What is wrong with the following code snippet? int main() { int width = 10; height...

    What is wrong with the following code snippet? int main() { int width = 10; height = 20.00; cout << "width = " << width << " height = " << height << endl; return 0; } The code snippet uses an uninitialized variable. The code snippet uses an undefined variable. The code snippet attempts to assign a decimal value to an integer variable. The code snippet attempts to assign an integer value to a decimal variable.

  • What will the following code printout? void Function(int *ptr) { *ptr = 11; } // In...

    What will the following code printout? void Function(int *ptr) { *ptr = 11; } // In Main... int values[] = {4, 10, 6, 12, 8, 14}; int *p = &values[4]; Function(p); for(int i = 2; i < 5; i++) cout << values[i] << ' '; a) 6 12 8 14 b) 6 12 8 c) 4 10 6 11 8 14 d) 6 12 11 e) 6 12 11 14 I know the answer is D, but I do not...

  • QUESTION 1 What is the output of the following code snippet? int main() { bool attendance...

    QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...

  • What is the likely results of running the following code fragment? Why? int *ptr = (int...

    What is the likely results of running the following code fragment? Why? int *ptr = (int *) 0xfeedbeef ; *ptr = 0 ; Suppose a class uses a dynamically-allocated integer array, pointed to by an int* variable named data. The current capacity of the array is stored in the integer variable capacity. The following code fragment is meant to dyamically resize the array, doubling its capacity. What is the major memory error and how would you detect it? int *tmp...

  • Computer Science C++ Program What is the output? int num =5; int* ptr = # cout...

    Computer Science C++ Program What is the output? int num =5; int* ptr = # cout << ptr << endl; cout << &ptr << endl; cout << *ptr << endl; What are the values stored in the following array? Draw a simple picture of the array with indexes and show what each element should be. int arrayExample [4] = { 5 };

  • Please, explain clearly each line of code with your answers. Question 1 Consider the following code...

    Please, explain clearly each line of code with your answers. Question 1 Consider the following code snippet: int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet? Question 2 Consider the following code snippet: int cnt = 0; int numarray[2][3]; for (int i = 0; i < 3; i++) {   for (int j =...

  • What is the value of n after the following snippet? int n = 13, m =...

    What is the value of n after the following snippet? int n = 13, m = 16; int* np = &n; np = &m; *np = 10; O 16 O 10 O A memory address O 13 True or False? This snippet correctly frees the memory it allocates. int** nums = new int*[10]; for (int i = 0; i < 10; i++) { nums[i] = new int; for (int i = 0; i < 10; i++) { delete nums[i]; delete...

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