03 Arrays Pointers
03 Arrays Pointers
Objectives
Be able to use arrays, pointers, and strings in
C programs
Be able to explain the representation of these
data types at the machine level, including
their similarities and differences
Cox
Arrays in C
All elements of same type homogenous
Unlike Java, array size in declaration
int array[10];
int b;
array[0]
array[9]
array[10]
array[-1]
=
=
=
=
3;
4;
5;
6;
Compare: C:
int array[10];
Java:
int[] array = new int[10];
First element (index 0)
Last element (index size - 1)
No bounds checking!
Allowed usually causes no error
array[10] may overwrite b
Cox
Array Representation
Homogeneous Each element same size s bytes
An array of m data values is a sequence of ms bytes
Indexing: 0th value at byte s0, 1st value at byte s1,
programmer
Cox
0x1008
a[2]
0x1004
a[1]
0x1000
a[0]
Arrays and Pointers
int a[3];
Array Representation
char
int
char
int
i
0x1014
0x1010
c2
0x100C
a[2]
0x1008
a[1]
0x1004
a[0]
0x1000
Cox
c1;
a[3];
c2;
i;
c1
Could be optimized
by making these
adjacent, and
reducing padding
(by default, not)
Array aligned by
size of elements
Array Sizes
int
array[10];
What is
sizeof(array[3])?
sizeof(array)?
40
Cox
Multi-Dimensional Arrays
int
matrix[2][3];
matrix[1][0] = 17;
0x1014
matrix[1][2]
0x1010
matrix[1][1]
0x100C
matrix[1][0]
0x1008
matrix[0][2]
0x1004
matrix[0][1]
0x1000
matrix[0][0]
Row Major
Organization
matrix[0][3] = 42;
Cox
Variable-Length Arrays
int
function(int n)
{
int array[n];
Memory Addresses
Storage cells are typically viewed as being
byte-sized
Usually the smallest addressable unit of memory
Few machines can directly address bits individually
Such addresses are sometimes called byte-
addresses
Cox
Pointers
Special case of bounded-size natural numbers
Maximum memory limited by processor word-size
232 bytes = 4GB, 264 bytes = 16 exabytes
int *ptr;
The variable ptr is a pointer to an int.
Cox
10
Pointer Operations in C
Creation
& variable
Dereference
* pointer
Indirect assignment
* pointer = val
11
Using Pointers
int i1;
int i2;
int *ptr1;
int *ptr2;
i1 = 1;
i2 = 2;
ptr1 = &i1;
ptr2 = ptr1;
0x1014
0x1010
ptr2:
0x100C
0x1008
ptr1:
0x1004
i2:
2
3
0x1000
i1:
3
1
0x1000
0x1000
*ptr1 = 3;
i2 = *ptr2;
Cox
12
int1
int2
= 1036;
= 8;
*/
*/
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;
What happens?
Type check warning: int_ptr2 is not an int
int1 becomes 8
Cox
13
int1
int2
= 1036;
= 8;
*/
*/
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;
What happens?
Type check warning: *int_ptr2 is not an int *
Changes int_ptr1 doesnt change int1
Cox
14
Pointer Arithmetic
pointer + number
E.g., pointer + 1
char
char
char
pointer number
adds 1 something to a pointer
*p;
a;
b;
p = &a;
p += 1;
int
int
int
In each, p now points to b
(Assuming compiler doesnt
reorder variables in memory)
Adds 1*sizeof(char) to
the memory address
*p;
a;
b;
p = &a;
p += 1;
Adds 1*sizeof(int) to
the memory address
15
Cox
16
Generic Pointers
void *: a pointer to anything
void
*p;
int
i;
char
c;
p = &i;
p = &c;
putchar(*(char *)p);
Cox
17
Pass-by-Reference
void
set_x_and_y(int *x,
int *y)
{
*x = 1001;
*y = 1002;
}
void
f(void)
{
int a = 1;
int b = 2;
set_x_and_y(&a,&b);
}
Cox
1001
1
1002
2
x
y
18
Dirty secret:
Array pointer to the initial
(0th) array element
a[i]
*(a+i)
Must explicitly
Really int *array pass the size
int
foo(int array[],
unsigned int size)
{
array[size - 1]
}
int
main(void)
{
int a[10], b[5];
foo(a, 10) foo(b, 5)
}
19
printf(%d\n, sizeof(array));
}
int
main(void)
{
int a[10], b[5];
foo(a, 10) foo(b, 5)
printf(%d\n, sizeof(a));
}
Cox
40
20
int
int
i;
array[10];
int *p;
int array[10];
for (p = array; p < &array[10]; p++)
{
*p = ;
}
Cox
21
Strings
In C, strings are just an array of characters
Terminated with \0 character
Arrays for bounded-length strings
Pointer for constant strings (or unknown length)
char str1[15] = Hello, world!\n;
char *str2
= Hello, world!\n;
C,
H e l l o ,
w o r l d ! \nterminator
C terminator: \0
Pascal, Java,
Cox
length
H e l l o ,
w o r l d ! \n
22
String length
Must calculate length:
array access
to pointer!
int
strlen(char str[])
{
int len = 0;
while (str[len] != \0)
len++;
return (len);
can pass an
array or pointer
Check for
terminator
23
int
main(int argc, char **argv)
{
...
}
an array/vector of
char *
Recall when passing an
array, a pointer to the
first element is passed
24
char **argv
3
0x1020
argv[4]
0x1018
argv[3]
0x1010
argv[2]
0x1008
argv[1]
hello
0x1000
argv[0]
./program
Cox
25
Next Time
Structures and Unions
Cox
26