Carrays
(Reek, Ch.8)
1
CS 3090: Safety Critical Programming in C
Review ofarrays
CS 3090: Safety Critical Programming in C
2
There are no array variables in C – only arraynamesEach name refers to a constant pointerSpace for array elements is allocated at declaration timeCan’t change where the array name refers to…but you can change the array elements,via pointer arithmeticintm[4];
m
Subscripts and pointer arithmetic
CS 3090: Safety Critical Programming in C
3
array[subscript]equivalent to*(array + (subscript))Strange but true: Given earlier declaration ofm, the expression2[m]is legal!Not only that: it’s equivalent to*(2+m)*(m+2)m[2]
Array names and pointer variables,playing together
CS 3090: Safety Critical Programming in C
4
intm[3];int*mid = m + 1;int*right = mid[1];int*left = mid[-1];int*beyond = mid[2];
beyond
mid
right
left
m
In C, arguments are passed “by value”A temporary copy of each argument is created, solely for use within the function callvoid f(intx,int*y) { … }void g(…) {inta = 17, b = 42;f(a, &b);…}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 thecallee(except via the return value)
Array names as function arguments
CS 3090: Safety Critical Programming in C
5
g
b
x
y
f
a
Array names as function arguments
CS 3090: Safety Critical Programming in C
6
But, functions that take arrays as arguments can exhibitwhat looks like“pass-by-reference” behavior, where the array passed in by thecalleedoes get changedRemember the special status of arrays in C –They are basically just pointers.So arrays are indeed passed by value –but only the pointer is copied, not the array elements!Note the advantage in efficiency (avoids a lot of copying)But – the pointer copy points to the same elements as thecallee’sarrayThese elements can easily be modified via pointer manipulation
Array names as function arguments
CS 3090: Safety Critical Programming in C
7
Thestrcpy“string copy” function puts this “pseudo” call-by-reference behavior to good usevoidstrcpy(char *buffer, char const *string);void f(…) {char original[4] = ″dog″;char copy[4];strcpy(copy, original);}
original
copy
f
string
buffer
strcpy
When can array size be omitted?
CS 3090: Safety Critical Programming in C
8
There are a couple of contexts in which an array declaration need not have a size specified:Parameter declaration:intstrlen(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:intvector[] = {1, 2, 3, 4, 5};In this case, just enough space is allocated to fit all (five) elements of theinitializerlist
Multidimensional arrays
CS 3090: Safety Critical Programming in C
9
How to interpret a declaration like:intd[2][4];This is an array with two elements:Each element is an array of fourintvaluesThe elements are laid out sequentially in memory, just like a one-dimensional arrayRow-major order: the elements of therightmostsubscript are stored contiguously
d[0][0]
d[0][1]
d[0][2]
d[0][3]
d[1][0]
d[1][1]
d[1][2]
d[1][3]
d[0]
d[1]
intd[2][4];d [1] [2]
Subscripting in a multidimensional array
CS 3090: Safety Critical Programming in C
10
d[0][0]
d[0][1]
d[0][2]
d[0][3]
d[1][0]
d[1][1]
d[1][2]
d[1][3]
d[0]
d[1]
*(d+1)
*(*(d+1)+2)
Why do we care about storage order?
CS 3090: Safety Critical Programming in C
11
If you keep within the “paradigm” of the multidimensional array, the order doesn’t matter…But if you use tricks with pointer arithmetic,it matters a lotIt also matters for initializationTo initializedlike this:use this:intd[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};rather than thisintd[2][4] = {0, 4, 1, 5, 2, 6, 3, 7};
Multidimensional arrays as parameters
CS 3090: Safety Critical Programming in C
12
Only the first subscript may be left unspecifiedvoid f(intmatrix[][10]); /* OK */voidg(int(*matrix)[10]); /* OK */voidh(intmatrix[][]); /* not OK */Why?Because the other sizes are needed for scaling when evaluating subscript expressions (see slide 10)This points out an important drawback to C:Arrays do not carry information about their own sizes!If array size is needed, you must supply it somehow(e.g., when passing an array argument, you often have to pass an additional “array size” argument) – bummer
0
Embed
Upload