Question

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 ++;

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

Code Snippet


   int a[4] = { 8, 3, 5, 6};
   int *ptr = a;
   ptr ++;

Break-down


   int a[4] = {8, 3, 5, 6};   //   array a is holding set of integers.

   int *ptr = a;           //   pointer ptr is containing the value of a (base address of array a).

   ptr ++;               //   pointer pts is incremented to its next position in array.


Output

   The ptr will first print the base address of the array, if used with * operator (like *ptr) it will print the value at first position in array i.e 8.


   After incrementing it is moved one step ahead and it will print next value in the array i.e 3.

Add a comment
Know the answer?
Add Answer to:
Consider the following code snippet. What will be printed by *ptr and ptr after the lines...
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
  • 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;

  • QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2,...

    QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2, 3};         int *p = a;         int **r = &p;         printf("%p %p", *r, a); A. Different memory addresses printed B. 1 2 C. Same memory address printed twice D. 1 1 2 points    QUESTION 10 What will be the output of following code snippet? int arr[4] = {1, 2, 3, 4};         int *p;         p = arr + 3;         *p = 5;         printf("%d\n", arr[3]); A....

  • 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...

  • 1. Given the code snippet below, what is the value of c that is printed? int...

    1. Given the code snippet below, what is the value of c that is printed? int a = 3, b = 4, c; a = 3; b = 4; a *= b; c = ++a * b--; cout << "c is: " << c ; Answer choices: 52, 32, 42

  • 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++ questions need help QUESTION 1 Given the following code, what is the value of *ptr?...

    c++ questions need help QUESTION 1 Given the following code, what is the value of *ptr? int var = 5; int *ptr = &var; int *myptr = ptr; *ptr = -8; var = 0; *myptr = 2; a. 2 b. -8 c. 5 d. 0 QUESTION 2 Given the following code, what is the value of ptr+1? int arr[3] = {0, 0, 0}; int *ptr = arr; *ptr = 8; a. 9 b. undefined c. the address of arr[0] d....

  • Consider the following code snippet. What will be stored in the list prices after this code...

    Consider the following code snippet. What will be stored in the list prices after this code executes? prices = [10.00, 15.50, 13.50, 20.15] for i in range(len(prices)) : prices[i] = prices[i] * 1.06 Group of answer choices [10.00, 15.50, 13.50, 20.15] [10.60, 16.43, 14.31, 21.36] [1.06, 1.06, 1.06, 1.06] [0.0, 0.0, 0.0, 0.0]

  • 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 output of the following code snippet? (If there is some kind of syntax...

    What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int laps = 8;        if (laps++ > --laps)               laps += 2;        else if (--laps > (laps - 1))               laps += 4;        else if (++laps)               laps -= 3;        cout << laps << endl; 2. What is the output of the following code snippet?     int j = 47;     int i = 8;     j =...

  • Please explain clearly your answers (step by step): 1) What is the output of the following...

    Please explain clearly your answers (step by step): 1) What is the output of the following code snippet? int my_fun (int A[], int size) { int result = 0;    for (int i = 0; i < size; i++)    {     result = result + A[i];    }    return result; } int main() { int myarr[10] = { 5, 6, 10, 1, 2, 4, 8, 1, 2, 1 }; for (int i = 0; i < 3; i++) {   cout << my_fun(myarr, i);...

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