C Programming – Part 3 Arrays and Strings. Collection of variables of the same type Individual...
-
Upload
oliver-dean -
Category
Documents
-
view
213 -
download
0
Transcript of C Programming – Part 3 Arrays and Strings. Collection of variables of the same type Individual...
TEC 284
C Programming – Part 3Arrays and Strings
What are arrays?
Collection of variables of the same type
Individual array elements are identified by an integer index
Array index always begins at 0The index is written in square
brackets
//Example of declaring a single dimension array
int values[10];
Declaring arrays
Normally when an array is declared, the type and the number of elements are specified
The compiler will then know how much memory to allocate for the array variable
It is possible to declare an array with dimensions that are not fixed immediately, but space can be allocated as required
This is done with pointersint *values;
Referencing array elements
In C, like most other languages, array indices start at o and end at one less than the array size
Eg int itemList [5];//Contains the following elementsitemList[0]itemList[1] itemList[2] itemList[3] itemList[4]
Getting the length of an array
No one function call or property which directly gives the length of an array in c as in other programming languages
To find the length of an array, you can do the following
int arrayItems[10];int numberOfItems = sizeof(arrayItems) /
sizeof(int);
Multi-Dimension Arrays
They are declared as follows:int results[20][5];
This ranges from results[0][0], results[0][1], results[0][3], results[0][4], results[1][0], results[1][1]…results[19][4]
Arrays can have even more dimensionsint results[20][10][5];
For further dimensions, simply add more []
Initializing arrays
int amounts[3];amounts[0] = 10;amounts[1] = 25;amounts[2] = 35;
This is normal initializationRemember here that the size of the
array is 3Only indices 0, 1 and 2 can be
referenced
Initializing arrays
int numbers[] = {1, 2, 3}; //sets up an array with size 3
int numbers[5] = {3, 4,8}; //sets up an array with size 5, but only sets the first three values
This is equivalent to the following: int numbers[5];
numbers[0] = 3;numbers[1]=4;numbers[2]=8;
Initializing arrays
You can use designated initializers to skip over elements of an array
int number[3]= { [0]=5, [2]=7};
Initialization of character arrays
You can initialize a one dimensional character array by specifying A brace-enclosed comma-separated list
of constants A string constant (with optional
surrounding braces) Initializing a string constant places
the null character(\0) at the end of the string if there is room or if the array dimensions are not specified
Initialization of character arrays
char name1[ ] = { 'J', 'a', 'n' };char name2[ ] = { "Jan" };char name3[4] = "Jan";
Initialization of multi-dimension arrays
int month_days [2][12];month_days[0][0] = 1;month_days[0][1] = 2;
int month_days[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
};
Strings
In C, strings are defined as arrays of characters char name[50];
C has no string handling facilities, so the following is illegal
char firstName[50], lastName[50], fullName[100];firstName = “John”; //illegallastName= “Doe”; //illegalfullName = firstName + lastName; //illegal
Strings
To print a string output, printf is used with the %s control character
printf(“%s”, name); In order to allow variable length strings
the \0 character is used to indicate the end of a string
So if we have a string char NAME[50]; and the name “DAVE” is stored in it, the contents will look like:
Exercise
Write a program which given a string and a character returns a count of the number of occurrences of that character
char find = ‘l’;char testString [] = “Hello World”;
Exercise
Write a program which loops over a character array and displays the vowels and a count of each of the vowels that are present in the array//Starting pointchar sentence[] = “I am enjoying learning C programming in TEC 284”;