Changeable Data - Variables
One variable holds one value
It occupies storage in memory
The contents of the storage = the value of the variable
There is a symbol (name) associated with its address
The value (contents) may be changed by the program instructions
Arrays in C To keep track of many things, you need many variables
E.g. int data1; int data2; int data3;
All of these variables need to have names
Based on slides © Pat Palmer
Modified by Diana Palsetia
Wh t if you need
What d tto kkeep ttrack
k off hundreds
h d d or th
thousands
d off
values?
CIT 593 2
Multiple Changeable Values Indexing into Arrays
Array To reference a single array element use arrayname [ index ]
Lets you associate one name with a lot of variables of same type for example:
randoms[4] contains the value 14
Example
randoms[9] contains the value 6
Locate by numerical index between 0 and array size minus 1 below
is an array of type int, declared as:
Indexed elements can be used just like simple variables
int randoms[10]; you can access their values
you can modify their values
0 1 2 3 4 5 6 7 8 9 An array index is sometimes called a subscript
randoms 12 43 6 83 14 -57
57 109 12 0 6
0 1 2 3 4 5 6 7 8 9
randoms 12 43 6 83 14 -57 109 12 0 6
CIT 593 3 CIT 593 4
1
Using Array Elements Declaring Array Size
0 1 2 3 4 5 6 7 8 9 When you declare an array, its maximum size must be
randoms 12 43 6 83 14 -57 109 12 0 6 known
F example,
For l you can declare:
d l
char columns[80];
code examples:
• x = randoms[1]; /* sets x to 43 */
• randoms[4] = 99; /* replaces 14 with 99 */
It’s better to avoid using a “magic number” :
• m = 5; #define MAXCOLUMNS 80
y = randoms[m]; /* sets y to –57 */ char columns[MAXCOLUMNS];
• z = randoms[randoms[9]]; /* sets z to 109 */
CIT 593 5 CIT 593 6
Every Array has One Type A program using an array
An array may hold any type of value #define MAX_ELEMS 5 total = 0.0;
for (ind=0;ind<MAX_ELEMS;ind++)
All values in an array must be the same type int main() {
{ total = total + randoms[ind];
For example, you can have: float randoms[MAX_ELEMS]; }
int week_days[7]; float total;
char small_letters[26]; float average; average = total / (float)MAX_ELEMS;
float real_nums[10]; int ind; /* loop counter */
printf("Total %f, Average %f\n",
You can also have an array of arrays: /* initialize the array */ total, average);
int matrix[2][3]; randoms[0]
d [0] = 34.0;
34 0
randoms[1] = 27.0;
return 0;
randoms[2] = 45.0;
randoms[3] = 82.0; }
randoms[4] = 22.0;
CIT 593 7 CIT 593 Total 210.000000, Average 42.000000 8
2
Array Assignment ? Passing
C passes arrays by address
Many languages let you assign the contents of one The address of the array (i.e., of the first element)
is written to the function's activation record
array to another
Otherwise, would have to copy each element
But C does not allow this int main()
{ Thi mustt be
This b a constant,
t t e.g.,
You have to use int numbers[MAX_NUMS]; #define MAX_NUMS 10
A for loop and go through the array yourself, explicitly copying …
each element between two arrays mean = average(numbers, MAX_NUMS);
…
}
int average(int values[], int size)
{
int index, sum = 0;
for (index = 0; index < size; index++) {
sum = sum + values[index];
}
return (sum / size);
}
CIT 593 9 CIT 593 10
Arrays Length Strings
No run-time length information
C doesn’t track length of arrays C does not have a native string type
No Java-like values.length construct Strings are faked in C using arrays of char
Thus, you need to pass length or use a sentinel
String literals (constants) are stored as arrays
The last used element in a string literal (array) is always \0 (NUL)
int average(int values[], int size)
{ The \0 value marks the end of a string within the full array
int index, sum;
for (index = 0; index < size; index++) {
sum = sum + values[index]; 0 1 2 3 4 5 6 7 8 9
} mychars 72 101 108 108 111 0 ? ? ? ?
return (sum / size);
} ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
CIT 593 11 CIT 593 12
3
Storage of a String Literal
0 1 2 3 4 5 6 7 8 9 10 11 12 13
72 101 108 108 111 32 87 111 114 108 100 10 0 ?
‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ‘ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘\n’ ‘\0’
this control character
(newline) was part of the
string to be printed out
NUL marks the end
of the string in
storage
CIT 593 13