- What output is printed by the “Example code” below?
- Assume that
arris an array of 5intelements. Is the codeint *p = arr + 5;legal? - Assume that
arris an array of 5intelements. Is the codeint *p = arr + 5; printf("%d\n", *p);legal? - What output is printed by the “Example code 2” below?
- Suppose we have variables
int ra1[10] = { 1, 2, 3};,int * ra2 = ra1;andint fun(int *ra);declarations. Willfun(ra1);compile? Willfun(ra2);compile? What if we change the function declaration toint fun(const int ra[]);?
// Example code
int arr[] = { 94, 69, 35, 72, 9 };
int *p = arr;
int *q = p + 3;
int *r = q - 1;
printf("%d %d %d\n", *p, *q, *r);
ptrdiff_t x = q - p;
ptrdiff_t y = r - p;
ptrdiff_t z = q - r;
printf("%d %d %d\n", (int)x, (int)y, (int)z);
ptrdiff_t m = p - q;
printf("%d\n", (int)m);
int c = (p < q);
int d = (q < p);
printf("%d %d\n", c, d);
// Example code 2
#include <stdio.h>
int sum(int a[], int n) {
int x = 0;
for (int i = 0; i < n; i++) {
x += a[i];
}
return x;
}
int main(void) {
int data[] = { 23, 59, 82, 42, 67, 89, 76, 44, 85, 81 };
int result = sum(data + 3, 4);
printf("result=%d\n", result);
return 0;
}