Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

__________________________________________________________________ *************

ID: 3687483 • Letter: #

Question

__________________________________________________________________

*************************(Text version below - same as above image)*************************

__________________________________________________________________

C PROGRAMMING - POINTERS (NOT C++)

1. If x is a variable and p is a pointer to x, which of the following expressions are aliases for x?

2. If x is an int variable and p and q are pointers to int, which of the following assignments are legal?

3. Write a line of code for each of the examples below. Assume that num1 and num2 are int variables and num1 is initialized to 5.

4. Suppose that the following declarations are in effect:

5. Suppose that num is a one dimensional array and p is a pointer variable. Assuming that the assignment p = num has just been performed, which of the following expressions are illegal because of mismatched types? Of the remaining expressions, which are true (have a nonzero value)?

a) *p c) *&p e) *x g) *&x b) &p d) &*p f) &x h) &*x

Explanation / Answer

1)

Therefore, the expressions ‘a’ and ‘g’ are alias for x.

2)

a) p refers address and x refers a value. Both are incompatible. Thus p=x is illegal.

b) *p refers a value. &x refers an address. Both are incompatible. Thus *p=&x is illegal.

c) &p refers address of a pointer and q refers an address. Both are incompatible. Thus &p=q is illegal.

d) &q refers address of a pointer and p refers an address. Both are incompatible. Thus p =&q is illegal.

e) p refers address and *&q refers a value. Both are incompatible. Thus p=*&q is illegal.

f) p and q both refers address. Thus p=q is legal.

g) p refers address and *q refers value. Both are incompatible. Thus p=*q is illegal.

h) *p refers value and q refers address. Both are incompatible. Thus *p=q is illegal.

i) *p and *q refers values. Thus *p=*q is legal.

Therefore, assignments f and i are legal.

3)

a)         int *p;

b)         p=&num1;

c)         printf("%d ",*p);

d)         num2=*p;

e)         printf("%d ",num2);

4)

a)

Therefore the value is 14.

b)

Therefore the value is 34.

c)

Therefore the value is 4

d)

Therefore the given statement is TRUE.

e)

Therefore the given statement is FALSE.

5)

Therefore correct option is a.