Arrays and Pointers in C: Alan L. Cox Alc@rice - Edu
Arrays and Pointers in C: Alan L. Cox Alc@rice - Edu
Alan L. Cox
[email protected]
Objectives
Be able to use arrays, pointers, and strings in
C programs
Be able to explain the representation o these
data types at the machine level, including
their similarities and dierences
Cox / Rixner Arrays and Pointers 2
Cox / Rixner Arrays and Pointers 3
Arrays in C
!o bounds chec"ing#
Allo$ed % usually causes no error
array&'() may over$rite b
*nli"e +ava, array si,e in declaration
int array[10];
int b;
array[0] = 3;
array[9] = 4;
array[10] = 5;
array[-1] = 6;
Compare- C- int array[10];
+ava- int[] array = new int[10];
All elements o same type % homogenous
.irst element /index (0
Last element /index si,e 1 '0
Cox / Rixner Arrays and Pointers 4
Array 2epresentation
3omogeneous 4ach element same si,e % s bytes
6ndexing- (
th
value at byte s (, '
st
value at byte s ', 7
m and s are not part o representation
B
CB
bytes A ;DB, B
E;
bytes A 'E exabytes
A pointer is just another "ind o value
A basic type in C
int ()tr;
The variable ptr is a pointer to an int.
Cox / Rixner Arrays and Pointers ""
Pointer Operations in C
Creation
& variable 2eturns variable?s memory address
=ereerence
* pointer 2eturns contents stored at address
6ndirect assignment
* pointer = val 8tores value at address
O course, still have...
Assignment
pointer = ptr 8tores pointer in another variable
Cox / Rixner Arrays and Pointers "2
*sing Pointers
int i1;
int i2;
int ()tr1;
int ()tr2;
i1 = 1;
i2 = 2;
)tr1 = *i1;
)tr2 = )tr1;
()tr1 = 3;
i2 = ()tr2;
i1+
i2+
)tr1+
0x1000
0x1004
0x1008
%
)tr2+
%
0x100C
0x1010
0x1014
1
2
0!1000
0!1000
3
3
Cox / Rixner Arrays and Pointers "3
*sing Pointers /cont.0
Fype chec" $arning- int,)tr2 is not an int
int1 becomes @
int int1 = 1036; -( soe .ata to )oint to (-
int int2 = /;
int (int,)tr1 = *int1; -( 'et a..resses of .ata (-
int (int,)tr2 = *int2;
(int,)tr1 = int,)tr2;
(int,)tr1 = int2;
9hat happens:
Cox / Rixner Arrays and Pointers "4
*sing Pointers /cont.0
Fype chec" $arning- (int,)tr2 is not an int (
Changes int,)tr1 % doesn?t change int1
int int1 = 1036; -( soe .ata to )oint to (-
int int2 = /;
int (int,)tr1 = *int1; -( 'et a..resses of .ata (-
int (int,)tr2 = *int2;
int,)tr1 = (int,)tr2;
int,)tr1 = int,)tr2;
9hat happens:
Cox / Rixner Arrays and Pointers "5
Pointer Arithmetic
pointer G number pointer % number
4.g., pointer 0 1 adds ' something to a pointer
char ();
char a;
char b;
) = *a;
) 0= 1;
int ();
int a;
int b;
) = *a;
) 0= 1;
6n each, p no$ points to b
/Assuming compiler doesn?t
reorder variables in memory0
Adds 'Hsi,eo/char0 to
the memory address
Adds 'Hsi,eo/int0 to
the memory address
Pointer arithmetic should be used cautiously
Cox / Rixner Arrays and Pointers "6
Fhe 8implest Pointer in C
8pecial constant pointer 1233
Points to no data
4#
... because array is really
a pointer
Cox / Rixner Arrays and Pointers 2"
Arrays and Pointers
int i;
int array[10];
for (i = 0; i 4 10; i00)
$
array[i] = %;
9
int ();
int array[10];
for () = array; ) 4 *array[10]; )00)
$
() = %;
9
These two blocks of code are functionally equivalent
Cox / Rixner Arrays and Pointers 22
8trings
6n C, strings are just an array o characters