C Arrays: (Reek, Ch. 8)
C Arrays: (Reek, Ch. 8)
(Reek, Ch. 8)
mid
int *right = mid[1]; (int [])
right
int *left = mid[-1]; (int [])
left
int *beyond = mid[2]; (int [])
void g(…) {
int a = 17, b = 42; 17 42 17
(int) (int) (int) (int [])
f(a, &b);
… a b x y
} g f
Pass-by-value is “safe” in that the function plays only in its
“sandbox” of temporary variables –
can’t alter the values of variables in the callee (except via the return
value)
original string
d
??? o
??? g
??? NUL
??? (char [])
(char []) (char) (char) (char) (char)
copy buffer
f strcpy
7 CS 3090: Safety Critical Programming in C
When can array size be omitted?
There are a couple of contexts in which an array
declaration need not have a size specified:
Parameter declaration:
int strlen(char string[]);
As we’ve seen, the elements of the array argument are not copied, so
the function doesn’t need to know how many elements there are.
Array initialization:
int vector[] = {1, 2, 3, 4, 5};
In this case, just enough space is allocated to fit all (five) elements of
the initializer list
d[0] d[1]
d [1] [2]
*(d+1)
*(*(d+1)+2) Then increment by the
size of 2 ints
d[0] d[1]