In: Computer Science
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?
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