-
What output is printed by the “Example code” below?
// 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);
-
Assume that
arr
is an array of 5int
elements. Is the codeint *p = arr + 5;
legal? -
Assume that
arr
is an array of 5int
elements. Is the codeint *p = arr + 5; printf("%d\n", *p);
legal? -
What output is printed by the “Example code 2” below?
// 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; }
-
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[]);
? -
What is the difference, if any, between
const int * p
andint * const p
? -
What size of a 2D array does the following define on the heap? Write the code to free memory to prevent memory leakages.
int **a = malloc(sizeof(int*) * 10); for (int i = 0; i < 10; i++) { a[i] = malloc(sizeof(int) * 5); }