CIS 180 – Programming in C / C++

Functions, Arrays, and Pointers Exercise

Values

ê /

A

ê
5 / 2 / 8 / 6 / 4 / 9 / 7 / 1 / 3 / 7
1022 / 1026 / 1030 / 1034 / 1038 / 1042 / 1046 / 1050 / 1054 / 1058
é
p

int Values[10] = {5, 2, 8, 6, 4, 9, 7, 1, 3, 7 };

int *p = Values;

The diagram above is used for questions 1 – 4 below

1.  Write 4 expressions that refer to the value of the element pointed to by letter “A” in the above diagram using array subscript/offset and pointer subscript/offset notation.

______

______

2.  What would be the value of the expression p + 3

______

3.  What would be the value of the expression *(Values + 5)

______

  1. What would be the value of the expression p[2] + Values[5]

______

  1. List the C++ operators permitted for pointer arithmetic?


______

  1. Write C++ statement(s) to: dynamically allocate memory for 100 integers using a pointer variable named ptr.

______

  1. Given the function prototype: int Cube ( int * ); Answer the following question: (Circle the correct answer.)

Would the function call Cube( &x ) where x is an integer variable in the calling function

generate a compile-time error or return some integer

8.  Given the declaration: int num[5]; Write a for loop to initialize the elements of the array to the values 10, 8, 6, 4, 2 using array-name/offset notation.

______

______

______

  1. Write a C++ statement to change the value of the integer variable pointed to by the pointer variable myPtr to 50.

______

  1. Write a C++ statement to declare a variable named dblPtr as a pointer to double and assign it the address of double variable DblVal.

______

Use the following code segment to answer questions 11 – 15.

1.
2. void Double ( int );
3. int Triple ( int * );
4. int Change ( int * );
5.
6. void main ( )
7. {
8. int Number = 5;
9. int Result = 8;
10. int Var = 2;
11.
12. Double ( Number );
13. Result = Triple ( &Number );
14. Var = Change ( &Number );
15.  Double ( Result );
16.  Result = Triple ( &Var );
17. }
18.
19. void Double ( int Var )
20. {
21. Var *= 2;
22. }
23.
24. int Triple ( int * ptr )
25. {
26. return *ptr * *ptr * *ptr;
27. }
28.
29. int Change ( int *Num )
30. {
31. static int Z = 5;
32.
33. Z = Z * 3;
34. *Num = *Num + Z;
35. return *Num;
36. } / 11. What would be the value of the variable Number after statement 13 finished executing?
______
12. What would be the value of the variable Var after statement 14 finished executing?
______
13. What would be the value of the variable Result after statement 15 finished executing?
______
14. What would be the value of the variable Result after statement 16 finished executing?
______
15. What would be the value of the variable Z in function Change after statement 16 finished executing?
______


Use the following information for questions 16 – 19. Read the statements carefully!!!

Given the declarations: int x = 10, *xPtr = &x; Assume the address for x is 1050 and for xPtr it is 1200.

All addresses are decimal for this exercise.

Display the output for the following statements.

16. cout < &x; ______

17. cout < xPtr; ______

18. cout < *xPtr * x; ______

19. cout < x; ______

For questions 20 – 25, circle T ( true ) or F ( false ).

20.  T F If the name of an integer array is to be passed to a function, the notation int [ ] or int * may be used interchangeably to declare the parameter’s type.

21.  T F * is called the indirection operator

22.  T F Array/Subscript and Pointer/Offset notation can be used interchangeably.

23.  T F A pointer must be declared to point to objects of a specific data type.

24.  T F Pointer variables provide another way to pass arguments to a function by reference.

25.  T F For a pointer variable, you can only get information about it’s value or it’s address in memory

For questions 26 – 30, select the best answer.

26. ______To print the value of the pointer variable myPtr , which statement would you use?

A. cout < &myPtr; D. cout < myPtr;

B. cout < *myPtr; E. cout < myPtr&;

C. cout < myPtr*;

27. ______Which of the following operations is NOT allowed for a pointer?

A. adding an integer to a pointer D. using a pointer variable in a relational expression

B. dereferencing a pointer E. getting the address of a pointer

C. multiplying a pointer by an integer

28. ______Which of the following values can NOT be assigned to a pointer to an integer variable.

A. The address of a float variable C. NULL

B. 0 ( zero ) D. The address of an integer variable

29. ______Given this function prototype: void square ( int * );

Which of the function calls below would be correct to call function square with integer variable y defined in the calling function.
A. square[y]; C. square ( &y );

B. square ( *y ); D. square ( y );

30. ______Which of the following statements would correctly declare Ptr as: a pointer to a double constant?


A. double const *Ptr; D. double * const Ptr;

B. const double * Ptr; E. const double * const Ptr;

C. double Ptr * const;