929 B
929 B
- If
i
is a variable andp
points toi
, which of the following expressions are aliases fori
?
a. *p
g. *&i
- If
i
is anint
variable andp
andq
are pointers toint
, which of the following assignments are legal?
e. p = *&q;
f. p = q;
i. *p = *q
- The following function supposedly computes the sum and average of the numbers in the array
a
, which has lengthn
.avg
andsum
point to the variables that the function should modify, unfortunately the function contains several errors, find and correct them.
void avg_sum(double a[], int n, double *avg, double *sum)
{
int i;
// This was assigning a pointer to a float,
// the dereference operator was missing
*sum = 0.0;
for (i = 0; i < n; i++)
// This wasn't increasing the value
// `sum` points to, it was modifying the address stored in the pointer
(*sum) += a[i];
// Missing dereference operators
*avg = *sum / n;
}