Compare commits
3 Commits
77e7cdf760
...
0870e5ab2b
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0870e5ab2b | ||
![]() |
0cbe502348 | ||
![]() |
2e3053784b |
@ -0,0 +1,20 @@
|
|||||||
|
1. Suppose that the following declarations are in effect:....
|
||||||
|
a. `14`
|
||||||
|
b. `34`
|
||||||
|
c. `4`
|
||||||
|
d. `true`
|
||||||
|
e. `false`
|
||||||
|
|
||||||
|
2. Suppose that `high`, `low`, and `middle` are all pointer variables of the same type, and the `low` and `high` point to elements of an array. Why is the following statement illegal, and how could it be fixed?
|
||||||
|
```c
|
||||||
|
middle = (low + high) / 2
|
||||||
|
```
|
||||||
|
The above statement is illegal because you can't add an `int *` to an `int *`. The below operation is legal because you can perform pointer subtraction, and because `low` is defined on the left hand side of the equation, then adding a long to a pointer is valid.
|
||||||
|
```c
|
||||||
|
middle = low + (high - low) / 2;
|
||||||
|
```
|
||||||
|
|
||||||
|
3. What will be the contents of the `a` array after the following statements are executed?
|
||||||
|
```c
|
||||||
|
{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
|
||||||
|
```
|
@ -1,20 +1,30 @@
|
|||||||
1. Suppose that the following declarations are in effect:....
|
> 1. If `i` is a variable and `p` points to `i`, which of the following expressions are aliases for `i`?
|
||||||
a. `14`
|
|
||||||
b. `34`
|
|
||||||
c. `4`
|
|
||||||
d. `true`
|
|
||||||
e. `false`
|
|
||||||
|
|
||||||
2. Suppose that `high`, `low`, and `middle` are all pointer variables of the same type, and the `low` and `high` point to elements of an array. Why is the following statement illegal, and how could it be fixed?
|
a. `*p`
|
||||||
```c
|
g. `*&i`
|
||||||
middle = (low + high) / 2
|
|
||||||
```
|
> 2. If `i` is an `int` variable and `p` and `q` are pointers to `int`, which of the following assignments are legal?
|
||||||
The above statement is illegal because you can't add an `int *` to an `int *`. The below operation is legal because you can perform pointer subtraction, and because `low` is defined on the left hand side of the equation, then adding a long to a pointer is valid.
|
|
||||||
```c
|
e. `p = *&q;`
|
||||||
middle = low + (high - low) / 2;
|
f. `p = q;`
|
||||||
```
|
i. `*p = *q`
|
||||||
|
|
||||||
|
> 3. The following function supposedly computes the sum and average of the numbers in the array `a`, which has length `n`. `avg` and `sum` point to the variables that the function should modify, unfortunately the function contains several errors, find and correct them.
|
||||||
|
|
||||||
3. What will be the contents of the `a` array after the following statements are executed?
|
|
||||||
```c
|
```c
|
||||||
{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user