Question

In: Computer Science

C++ Assignment: Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator...

C++ Assignment:

Chapter 9 (Pointers) – Review Questions

Short Answer

1.     What does the indirection operator do?

2.     Look at the following code.

int x = 7;

int *ptr = &x;

What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout?

3.     So far you have learned three different uses for the * operator. What are they?

4.     What math operations are allowed on pointers?

5.     Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?

6.     Look at the following array definition.

int numbers[] = {2, 4, 6, 8, 10};

What will the following statement display?

cout << *(numbers + 3) << endl;

7.     What is the purpose of the new operator?

8.     What is the purpose of the delete operator?

9.     Under what circumstances can you successfully return a pointer from a function?

10. What is the difference between a pointer to a constant and a constant pointer?

11. What are two advantages of declaring a pointer parameter as a constant pointer?

Solutions

Expert Solution

Hi,

Q.1. What does the indirection operator do?

Dereferences the pointer; allows you to work with the value the pointer is pointing to.

Indirection operator(*) is also known as dereference operator. it is a unary operator , means this operator works with single operand and that include pointer variables. indirector operator returns the value of the variable located at the address specified by its operand.

suppose we declared a var

   int  *ptr;

int val;

int num = 3000;

   // take the address of num
   ptr = &num ;
   // take the value available at ptr
   val = *ptr;

not if we will print num, val and ptr then we will get value like 3000, 0xbff64494 and 3000 .

ptr contains address of variabble num

Q.2   

It will displayed like

Value of *ptr:7
Value of ptr :0x7ffdc6b06e84

Q.3  So far you have learned three different uses for the * operator. What are they?

As per above program example in Q.1

1. You should read the & operator as "the address of" which means &num will be read as "the address of num".

2. The second operator is indirection Operator *, and it is the complement of &. It is a unary operator that returns the value of the variable located at the address specified by its operand.

3. (ptr) will return the address of stored value

Q. 4 What math operations are allowed on pointers?

As we know so that pointer is an address which contains numeric value so you can perform arithmatic operation on pointer same as you do with othr numeric values.The arithmatic operation we can perform on pointer are

++ Incrementing a Pointer like ptr++ ( point to the next location)

-- Decrementing a Pointer like ptr-- (point to the previous location )

+  Addition like ptr += 1, it means we have added 1 to the pointer variable.The result shows that it points to the next element.

-  Subtraction like ptr -= 1, it means we have subtract -1 from the pointer variable and it will now point to the previous element.

Q.5 Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?

It multiplies 4 by the number of bytes for an int then adds it to the memory addresss.

Q.6

it will display 8 when you will rn below program that means it will point to the element 8 in given array if you will perfrom + addition arithmatic operator.

int main()
{
int number[]={2,4,6,8,10};
cout<<*(number+3)<<endl;
return 0;
}

Q.7 What is the purpose of the new operator?

Used for Dynamic Memory Allocation to set aside a chunk of memory for a specific data type.supports dynamic allocation of objects using new operator.

Q.8 What is the purpose of the delete operator?

Supports dynamic deallocation of objects using the delete operators.Free the memory that was created by the new operator.

Q.9 Under what circumstances can you successfully return a pointer from a function?

Thereare two way to return the pointer from function:

(a). A pointer to an item that was passed into the function as an argument
(b) . A pointer to a dynamically allocated chunk of memory

Q.10 What is the difference between a pointer to a constant and a constant pointer?

Pointer to a constant:

A pointer points to a constant value, it cannot change any values inside the constant.


Constant pointer:

Once the pointer is initialized with an address, it cannot point to anything else.it will be same without any changes

Q 11. What are two advantages of declaring a pointer parameter as a constant pointer?

A. The parameter will be initialized with the address that is passed as an argument into it.
B. Cannot be changed to point to anything else while the function is executing.

Thanks



Related Solutions

Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.    ...
Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.     Look at the following code. int x = 7; int *ptr = &x; What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout? 3.     So far you have learned three different uses for the * operator. What are they? 4.     What math operations are allowed on pointers? 5.     Assuming ptr is a...
Chapter 9 (Pointers) - Review Questions Find the Error Each of the following definitions and program...
Chapter 9 (Pointers) - Review Questions Find the Error Each of the following definitions and program segments has errors. Locate as many as you can. 46. int ptr* = nullptr; 47. int x, *ptr = nullptr; &x = ptr; 48. int x, *ptr = nullptr; *ptr = &x; 49. int x, *ptr = nullptr; ptr = &x; ptr = 100; //Store 100 in x cout << x << endl; 50. int numbers[] = {10, 20, 30, 40, 50}; cout <<...
WEEK 9 ASSIGNMENT Answer the following questions: With IT projects – what are some of the...
WEEK 9 ASSIGNMENT Answer the following questions: With IT projects – what are some of the common risks associated with managing an IT project? What are the potential consequences of not assessing the technical risks associated with an information systems development project? In what ways could you identify an information systems project that was technically riskier than another?
To kill a mockingbird chapter 9 : answer the following questions in complete sentences: 1.What advice...
To kill a mockingbird chapter 9 : answer the following questions in complete sentences: 1.What advice does Atticus have for Scout? 2. Scout and Jem have “mixed feelings” about Christmas. What are these feelings and why? 3. Why is Aunt Alexandra critical of the way Atticus is raising Scout? In light of this criticism, what is ironic about the fact that Francis is learning to cook? 4. How does Scout respond to Aunt Alexandra’s vision of what it means to...
Answer the following questions: What is the purpose of #ifndef NO_CHECK in operator[]? Why does void...
Answer the following questions: What is the purpose of #ifndef NO_CHECK in operator[]? Why does void push_back(Object&& x) use std::move(x) while void push_back(const Object& x) simply uses objects[theSize++] = x;? What does return &objects[0]; do? code: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <stdexcept> #include "dsexceptions.h" template<typename Object> class Vector { public: explicit Vector(int initSize = 0) : theSize{initSize}, theCapacity{initSize + SPARE_CAPACITY} { objects = new Object[theCapacity]; } Vector(const Vector &rhs) : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{nullptr} { objects =...
Review the four financial statements discussed in Chapter 1. Then answer the following questions in your...
Review the four financial statements discussed in Chapter 1. Then answer the following questions in your initial response: Choose one of the financial statements you reviewed and explain its purpose. Explain what type of information can be derived from the financial statement you chose. What do you think is the major purpose of all financial statements? What is the importance of accounting in a health care manager’s role? How will you use it in your health care career? Word Count:...
Short Answer Questions: 1.) What does the term "economizing problem" mean for an individual and for...
Short Answer Questions: 1.) What does the term "economizing problem" mean for an individual and for society? As part of your answer, include the four economic resources and the opportunity cost in your answer. 2.) What are the four assumptions in the production possibilities curve and how can unemployment be illustrated with that curve. You do not need to draw it, you need to explain it. Additionally, what does it mean when the curve shifts to the right? 3.) Explain...
This questions are from Evolution class 1. In a short answer: What does the evidence point...
This questions are from Evolution class 1. In a short answer: What does the evidence point to as the ancestors of eukaryotes? How did eukaryotes form? Your answer 2. Mitochondria have their own Genes; in humans, only 13 genes. What is significant about this related to evolutionary origin of mitochondria AND why might be an ecological/evolutionary reason for there be so few mitochondrial Genes? 3. What is the principle of the selfish gene and how is our common way of...
Short Answer – Please answer these questions with a sentence or two. 5. What does an...
Short Answer – Please answer these questions with a sentence or two. 5. What does an e-mail link do? 6.What are the four basic principles of Web Design? 7. What is the purpose of <thead>, <tbody>, <tfoot> elements 8. What is the difference between an element and an attribute?
Using Buck Chapter 17 “Cardiovascular System”; review the objectives, key terms and chapter information and answer the following review questions.
Assignment - Theory "Cardiovascular System"Using Buck Chapter 17 “Cardiovascular System”; review the objectives, key terms and chapter information and answer the following review questions. Copy and Paste your assignment on a separate MS word document or compatible file, and submit your completed assignment electronically via Moodle prior to the due date.All rules of grammar, punctuation and spelling must be observed. Please highlight your answers.The term that describes the procedure in which the surgeon withdraws fluid from the pericardial space by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT