Download - Arrays.ppt
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 1/12
C arrays
(Reek, Ch. 8)
1 CS 3090: Safety Critical Programmingin C
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 2/12
Review of arrays
CS 3090: Safety Critical Programmingin C
2
There are no array variables in C – only arraynames Each name refers to a constant pointer
Space for array elements is allocate at eclarationtime
Can!t change "here the array name refers to# b$t yo$ can change the array elements%
via pointer arithmetic
int m[4];
(int [])
m
???(int)
???(int)
???(int)
???(int)
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 3/12
Subscripts and pointer arithmetic
CS 3090: Safety Critical Programmingin C
3
array[subscript] e&$ivalent to *(array +(subscript))
Strange b$t tr$e: 'iven earlier eclaration of m%
the e(pression 2[m] is legal) *ot only that: it!s e&$ivalent to *(2+m)
*(m+2)
m[2]
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 4/12
Array names and pointer variables,playing together
CS 3090: Safety Critical Programmingin C
+
int m[3];
int *mid = m + 1;
int *right = mid[1];
int *let = mid[!1];
int *bey"nd = mid[2];
(int [])
bey"nd
???(int)
???(int)
???(int)
(int [])
(int [])
(int [])
(int [])
mid
right
let
m
s$bscript ,- "ith pointer
variable
compiler may not catch this –r$ntime environment certainly
"on!t
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 5/12
.n C% arg$ments are passe /by val$e temporary copy of each arg$ment is create% solelyfor $se "ithin the f$nction call
#"id (int $% int *y) & '
#"id g(') & int a = 1% b = 42;
(a% b);
'
Passbyval$e is /safe in that the f$nction playsonly in its /sanbo( of temporary variables – can!t alter the val$es of variables in the callee e(cept
via the ret$rn val$e4
Array names as function arguments
CS 3090: Safety Critical Programmingin C
5
1(int)
42(int)
g
b
1(int)
$ y
(int [])
a
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 6/12
Array names as function arguments
CS 3090: Safety Critical Programmingin C
6
7$t% f$nctions that ta8e arrays as arg$ments cane(hibit what looks like /passbyreferencebehavior% "here the array passe in by the callee
oes get change emember the special stat$s of arrays in C –
They are basically $st pointers;
So arrays are inee passe by val$e –
b$t only the pointer is copie% not the array elements) *ote the avantage in e<ciency avois a lot of copying4
7$t – the pointer copy points to the same elements as thecallee!s array
These elements can easily be moi=e via pointermanip$lation
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 7/12
Array names as function arguments
CS 3090: Safety Critical Programmingin C
>
The strcpy /string copy f$nction p$ts this/pse$o callbyreference behavior to goo$se
#"id strcpy(char *buer% char c"nst *string);
#"id (') & char "riginal[4] = d"g;
char c"py[4];
strcpy(c"py% "riginal);
(char [])
"riginal
d(char)
"(char)
g(char)
,-.(char)
(char [])
c"py
???(char)
???(char)
???(char)
???(char)
(char [])
string
(char [])
buer
strcpy
d(char)
"(char)
g(char)
,-.(char)
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 8/12
When can array size be omitted?
CS 3090: Safety Critical Programmingin C
?
There are a co$ple of conte(ts in "hich anarray eclaration nee not have a si@especi=e: Parameter eclaration:
int strlen(char string[]); s "e!ve seen% the elements of the array arg$ment are
not copie% so the f$nction oesn!t nee to 8no" ho"many elements there are;
rray initiali@ation:
int #ect"r[] = &1% 2% 3% 4% /; .n this case% $st eno$gh space is allocate to =t all
=ve4 elements of the initiali@er list
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 9/12
Multidimensional arrays
CS 3090: Safety Critical Programmingin C
9
Ao" to interpret a eclaration li8e:int d[2][4];
This is an array "ith t"o elements: Each element is an array of fo$r int val$es
The elements are lai o$t se&$entially inmemory% $st li8e a oneimensional array o"maor orer: the elements of the rightmost
s$bscript are store contig$o$sly
(int) (int) (int) (int) (int) (int) (int) (int)
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]
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 10/12
int d[2][4];
d [1] [2]
Subscripting in a multidimensional array
CS 3090: Safety Critical Programmingin C
10
(int) (int) (int) (int) (int) (int) (int) (int)
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)
.ncrement by the si@eof
1 array of + ints
Then increment by the
si@e of 2 ints
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 11/12
Why do we care about storage order?
CS 3090: Safety Critical Programmingin C
11
.f yo$ 8eep "ithin the /paraigm of them$ltiimensional array% the orer oesn!tmatter#
7$t if yo$ $se tric8s "ith pointer arithmetic%
it matters a lot .t also matters for initiali@ation To initiali@e d li8e this:
$se this:
int d[2][4] = &0% 1% 2% 3% 4% /% % ; rather than this
int d[2][4] = &0% 4% 1% /% 2% % 3% ;
0 1 2 3
+ 5 6 >
7/18/2019 Arrays.ppt
http://slidepdf.com/reader/full/arraysppt-56d480183d460 12/12
Multidimensional arrays as parameters
CS 3090: Safety Critical Programmingin C
12
,nly the =rst s$bscript may be left $nspeci=e#"id (int matri$[][10]); * *
#"id g(int (*matri$)[10]); * *
#"id h(int matri$[][]); * n"t *
Bhy 7eca$se the other si@es are neee for scaling "hen
eval$ating s$bscript e(pressions see slie 104
This points o$t an important ra"bac8 to C:
rrays o not carry information abo$t their o"n si@es)
.f array si@e is neee% yo$ m$st s$pply it someho"e;g;% "hen passing an array arg$ment% yo$ often haveto pass an aitional /array si@e arg$ment4 –b$mmer