Arrays in C & C++: (Including A Brief Introduction To Pointers)
Arrays in C & C++: (Including A Brief Introduction To Pointers)
Reading Assignment
Chapter 6 of Deitel & Deitel Chapter 5 of Kernighan & Ritchie
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!
For ease of access to any member of array For passing to functions as a group
CS-2303, C-Term 2010 Arrays in C & C++ 3
Arrays in C
By far, the dominant kind of data structure in C programs Many, many uses of all kinds
Collections of all kinds of data Instant access to any element
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 of structs, unions, pointers, etc., are also allowed Array indexes always start at zero in C
CS-2303, C-Term 2010 Arrays in C & C++ 5
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-2303, C-Term 2010 Arrays in C & C++ 6
Two-dimensional Arrays
int D[10][20] A one-dimensional array with 10 elements, each of which is an array with 20 elements I.e., int D[10][20] /*[row][col]*/
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-2303, C-Term 2010 Arrays in C & C++ 8
Questions
13
Declaring Arrays
Static or automatic Array size may be determined explicitly or implicitly
14
/*not visible to
15
/*not visible to
16
address space
0x00000000
17
} //f
CS-2303, C-Term 2010 Arrays in C & C++ 18
address space
0x00000000
19
} //f
CS-2303, C-Term 2010 Arrays in C & C++ 20
} //func
Array Initialization
int A[5] = {2, 4, 8, 16, 32};
Static or automatic
Values must be compile-time constants (for static arrays) Values may be run-time expressions (for automatic arrays) See p. 86 of K&R
CS-2303, C-Term 2010 Arrays in C & C++ 23
required by operand
See p.135 of K&R, 7.7 of D&D
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)
24
required by operand
See p.135
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)
25
OR
static char daytab[2][12] = { 31,28,31,30,31,30,31,31,30,31,30,31, 31,29,31,30,31,30,31,31,30,31,30,31 }; // daytab
CS-2303, C-Term 2010 Arrays in C & C++ 26
Questions?
27
0 1 2 3 4 5 6 7 8 9 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-2303, C-Term 2010 Arrays in C & C++ 28
0 1 2 3 4 5 6 7 8 9 10 11 2n-1
(Almost) always aligned to multiple of size in bytes Address is first byte of sequence (i.e., byte zero)
May be low-order or high-order byte Big endian or Little endian
CS-2303, C-Term 2010 Arrays in C & C++ 29
Definition Pointer
11 0 1 2 3 4 5 6 7 8 9 10 11
2n-1
A value indicating the number of (the first byte of) a data object
Also called an Address or a Location
Memory Addressing
0xFFFFFFFF stack (dynamically allocated) SP
address space
0x00000000
31
Pointers in C
Used everywhere
For building useful, interesting, data structures For returning data from functions For managing arrays
Declaring Pointers in C
a pointer to an int double *q; a pointer to a double char **r; a pointer to a pointer to a
int *p;
char
type *s;
33
int * const q;
q is a constant pointer to an integer variable I.e., pointer cannot change, thing it points to can!
Pointer Arithmetic
int *p, *q; q = p + 1;
Construct a pointer to the next integer after *p and assign it to q
Why introduce pointers in the middle of a lesson on arrays? Arrays and pointers are closely related in C
In fact, they are essentially the same thing! Esp. when used as parameters of functions
Only difference:
double A[10] sets aside ten units of memory, each large enough to hold a double, and A is initialized to
point to the zeroth unit. double *A sets aside one pointer-sized unit of memory, not initialized
You are expected to come up with the memory elsewhere!
Note: all pointer variables are the same size in any given machine architecture
Regardless of what types they point to
CS-2303, C-Term 2010 Arrays in C & C++ 39
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
Are identical function prototypes! Pointer is passed by value I.e. caller copies the value of a pointer to float into the parameter A Called function can reference through that pointer to reach thing pointed to
CS-2303, C-Term 2010 Arrays in C & C++ 41
Examples
while ((rc = scanf("%lf", &array[count])) !=EOF && rc!=0) double getLargest(const double A[], const int sizeA) { double d; if (sizeA > 0) { d = getLargest(&A[1], sizeA-1); return (d > A[0]) ? d : A[0]; } else return A[0]; } // getLargest
CS-2303, C-Term 2010 Arrays in C & C++ 43
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
44
Safety Note
When passing arrays to functions, it is recommended to 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-2303, C-Term 2010 Arrays in C & C++ 45
Questions?
46