Professional Documents
Culture Documents
Definition Array
A collection of objects of the same type
stored contiguously in memory under one
name
May be type of any kind of variable
May even be collection of arrays!
Arrays in C
Examples
int A[10]
An array of ten integers
A[0], A[1], , A[9]
double B[20]
An array of twenty long floating point numbers
B[0], B[1], , B[19]
Arrays in C
Examples (continued)
int C[]
An array of an unknown number of integers
(allowable in a parameter of a function)
C[0], C[1], , C[max-1]
int D[10][20]
An array of ten rows, each of which is an array of
twenty integers
D[0][0], D[0][1], , D[1][0], D[1][1],
, D[9][19]
Not used so often as arrays of pointers
CS-2301, B-Term 2009
Arrays in C
Array Element
May be used wherever a variable of the
same type may be used
In an expression (including arguments)
On left side of assignment
Examples:
A[3] = x + y;
x = y A[3];
z = sin(A[i]) + cos(B[j]);
CS-2301, B-Term 2009
Arrays in C
Arrays in C
Arrays in C
Caution!
C does NOT check array bounds
I.e., whether index points to an element within the
array
Might be high (beyond the end) or negative (before
the array starts)
Arrays in C
Declaring Arrays
Static or automatic
Array size determined explicitly or
implicitly
Array size may be determined at run-time
Arrays in C
#define CLASS_SIZE 73
double B[CLASS_SIZE];
const int nElements = 25
float C[nElements];
static char D[256];
linker */
CS-2301, B-Term 2009
Arrays in C
/*not visible to
10
stack
(dynamically allocated)
SP
Static
array
s llo
wheaddress
ca
n pro aspace
gram ted here
is loa
ded.
0x00000000
heap
(dynamically allocated)
static data
program code
(text)
Arrays in C
PC
11
} //f
CS-2301, B-Term 2009
Arrays in C
12
Auto
matic
array
here
s allo
upon
cated
entry
to blo
0xFFFFFFFF
ck.
stack
(dynamically allocated)
SP
address space
heap
(dynamically allocated)
static data
0x00000000
program code
(text)
Arrays in C
PC
13
in
d
e
ca t n
o
l
l
s a a wh e
i
y
arra ta are ed
s
i
Th tic da
ad
o
l
sta ram is
prog
} //f
CS-2301, B-Term 2009
Arrays in C
14
} //func
Arrays in C
15
Array Initialization
int A[5] = {2, 4, 8, 16, 32};
Static or automatic
Arrays in C
16
Arrays in C
17
required by operand
Examples:
sizeof (int) # of bytes per int
sizeof (float) # of bytes per float
sizeof days # of bytes in array days (previous slide)
# of elements in days = (sizeof days)/sizeof(int)
Arrays in C
18
es
h
t
n
required by operand
are
p
h
it
pe
w
y
t
f
See p.135
eo of the
z
i
es
s
e
s
z
e
i
h
s
n t j ect
is
e
r
a
Examples:
no p the ob
f
of
oper
sizeof (int) # of bytes
e
e
int
z
z
si
si
s
n
a
me per float
sizeof (float) # of bytes
sizeof days # of bytes in array days (previous slide)
# of elements in days = (sizeof days)/sizeof(int)
Arrays in C
19
10
11
2n-1
Definitions:
Byte: an 8-bit memory cell capable of storing a value in
range 0 255
Address: number by which a memory cell is identified
CS-2301, B-Term 2009
Arrays in C
20
10
11
Arrays in C
21
2n-1
Definition Pointer
11
0
10
11
2n-1
Arrays in C
22
Memory Addressing
0xFFFFFFFF
stack
(dynamically allocated)
SP
address space
0x00000000
heap
(dynamically allocated)
s of
sse 3 2 e
r
d
ad s i n a
e
h
on cture
e t static
data
i
r
t
a
a
c
o
se
te
The mory l e archi
me program
hin code
c
a
bit m
PC
(text)
Arrays in C
23
Pointers in C
Used everywhere
&'
'
ry
in a D )
b
s
a
N
e
A
e
m
For building useful,
interesting,
data structures
s
i
sa
w
e
t
i
h
t
(b functions
r
o
For returning
Not data
t
from
pera
o
For managing arrays
Arrays in C
24
Declaring Pointers in C
int *p;
a pointer to an int
double *q; a pointer to a double
char **r; a pointer to a pointer to a
char
type *s;
a pointer to an object of
type type
Arrays in C
25
int * const q;
q is a constant pointer to an integer variable
I.e., pointer cannot change, thing it points to can!
Arrays in C
26
Pointer Arithmetic
int *p, *q;
q = p + 1;
Construct a pointer to the next integer after *p
and assign it to q
Arrays in C
27
Arrays in C
28
th e id
t
a
th
al
s
v
k
s
c
i
che ointer
r
e
p
ev
C n ulting
res
Arrays in C
29
int A[10];
int *p;
Type of A is int *
p = A; and A = p; are legal assignments
*p refers to A[0]
*(p + n) refers to A[n]
p = &A[5]; is the same as p = A + 5;
CS-2301, B-Term 2009
Arrays in C
30
Only difference:
double A[10] sets aside ten units of memory, each
large enough to hold a double
double *A sets aside one pointer-sized unit of
memory
You are expected to come up with the memory elsewhere!
Arrays in C
31
Note
C does not assign arrays to each other
E.g,
double A[10];
double B[10];
A = B;
assigns the pointer value B to the pointer value A
Contents of array A are untouched
Arrays in C
32
Arrays in C
33
//init
Arrays in C
34
Examples
while ((rc = scanf("%lf",
&array[count])) !=EOF && rc==0)
Arrays in C
35
Result
Even though all arguments are passed by
value to functions
pointers allow functions to assign back
to data of caller
Arrays are pointers passed by value
Arrays in C
36
Safety Note
When passing arrays to functions, always
specify const if you dont want function
changing the value of any elements
Reason: you dont know whether your
function would pass array to another before
returning to you
Exception many software packages dont specify
const in their own headers, so you cant either!
CS-2301, B-Term 2009
Arrays in C
37