Arrays in C 1
One Dimensional Array (Vector) Ordered List of Values All of the same type Individual elements accessible by index Vector has a Size (Number of elements) 0 1 2 3 4 5 17.3 14.5 3.2 12.0 5.65 14.5 2
One Dimensional Array Declaration type name[size]; type - Any built-in or derived data type int, char, short, float, etc. name - Any valid variable name e.g. vx, vy, myarray, etc. etc. size - Integer constant - how many items are in the list WARNING: Indeces are 0,1,2,3,, (size-1)! Vectors must be declared before they are used. 3
Referencing Vector Values name[index] name - The (declared) name of a vector index - The index of a specific element in the vector First element in the vector is vec[0] Last element in the vector is vec[size-1] Index may be any valid integer expression e.g. : vec[3], vec[j], vec[2*i+1] 4
Example Vector Code int grades[14]; // Code with fills in grades goes here int j,sum=0; for(j=0; j<14; j++) { sum +=grades[j]; } float avg=(float)sum/14; printf( Average grade: %f\n,avg); 5
Initializing a Vector int x=7; // Initializing a scalar variable int gpc[12]={4,4,6,4,3,3,2,2,3,1,4,4}; // Initializing vector 0 1 2 3 4 5 6 7 8 9 10 11 4 4 6 4 3 3 2 2 3 1 4 4 Or, let the compiler count the number of elements int gpc[ ]= {4,4,6,4,3,3,2,2,3,1,4,4}; IF ARRAY IS NOT INITIALIZED IT S INITIAL VALUE IS UNKNOWN! 6
Matrix Two Dimensional Array Declaration: type name[rows][cols]; Reference: name[row_index][col_index] 0 <= row_index < rows 0 <= col_index < cols Indexes may be any integer expressions 7
Array Dimensions Matrix: int matrix[2][3]={10,11,12,20,21,22} Vector: int vec[4]={10,20,30,40}; vec[0] vec[1] vec[2] vec[3] 10 20 30 40 matrix[0][0] 10 matrix[1][0] 20 matrix[0][1] 11 matrix[1][1] 21 matrix[0][2] 12 matrix[1][2] 22 Cube: char cube[3][2][3] = { abcdefghijklmnopqr }; [2][0][0] m [2][0][1] n [2][0][2] o [1][0][0] g [1][0][1] h [1][0][2] i [2][1][0] p [2][1][1] q [2][1][2] r [0][0][0] a [0][0][1] b [0][0][2] c [1][1][0] j [1][1][1] k [1][1][2] l [0][1][0] d [0][1][1] e [0][1][2] f 8
Array Values are Contiguous Right next to each other in memory int vec[6] int m [4][3]; vec[0] vec[1] vec[2] vec[3] vec[4] vec[5] m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0] m[2][1] m[2][2] m[3][0] m[3][1] m[3][2] 9
Row Major Order Think of multi-dimensional indexes as an odometer Rightmost digit of index increases the fastest Once rightmost digit reaches it s limit, it goes back to zero, and Digit to the left increases by 1 int m[4][3]={0,1,2,10,11,12, 20,21,22,30,31,32}; 0 1 2 0 0 1 2 1 10 11 12 2 20 21 22 3 30 31 32 m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0] m[2][1] m[2][2] m[3][0] m[3][1] m[3][2] 0 1 2 10 11 12 20 21 22 30 31 32 10
Array Bounds Checking vec[0] 4 int vec[5]; int i; for(i=0;i<=5;i++) vec[i]=4; vec[1] 4 vec[2] 4 vec[3] 4 vec[4] 4 i 4 NO RUN-TIME ARRAY BOUNDS CHECKING IN C!!!!!!!!!!!! Trust the programmer, and save the run-time! Programmer must be trustworthy! Writing past the end of an array can cause many problems May write over other variables May cause a segmentation violation 11
Strings in C 12
What is a string? A string is just a vector of ASCII characters Followed by a null terminator a byte with the value 0x00 char str[14]= This a string ; { T, h, i, s,, a,, s, t, r, i, n, g, x00} Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ASCII T h i s a s t r i n g Hex x54 x68 x69 x73 x20 x61 x20 x73 x74 x72 x69 x6e x67 x00 13
The null terminator A null terminator is a single byte with the value 0b00000000 A null terminator is NOT a null address not NULL! 0x0000000000000000 A null terminator is NOT an integer 0 0x000000000 When we assign a word 0 to a char 0, the left bits are truncated So we can assign 0 to a char to get a null terminator When we compare a null-terminator to a word 0, the null terminator is extended to word length So we can compare a byte to 0 to see if it is a null terminator To avoid confusion, I use 0x00 as a null terminator (0b00000000) You will also see \0 the ASCII escaped null terminator (0b00000000)
printf substitutes a string for %s char str[14]= This a string ; printf( Variable str contains: %s : and no more\n,str); Variable str contains: This a string : and no more 15
Empty String char str[14]= This a string ; str[0]=x00; printf( Variable str contains: %s : and no more\n,str); Variable str contains: : and no more Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ASCII h i s a s t r i n g Hex x00 x68 x69 x73 x20 x61 x20 x73 x74 x72 x69 x6e x67 x00 16
Standard Library String Functions #include <string.h> char str[18]= This a string ; printf( Size of str buffer: %d\n,sizeof(str)); printf( Length of str string: %d\n,strlen(str)); Size of str buffer: 18 Length of str string: 13 Indx 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 char T h i s a s t r i n g Hex x54 x68 x69 x73 x20 x61 x20 x73 x74 x72 x69 x6e x67 x00 x00 x00 x00 x00 17
strlen(char str[ ]); Returns the number of bytes up to (but not including) the null terminator of the argument string. Because we start indexes at zero: str[strlen(str)]==x00 // ALWAYS TRUE! char empty[20]= ; printf( Length of empty: %d\n,strlen(empty)); Length of empty: 0 18
strcpy(char to[ ],char from[ ]) Copies from string to to string ASSUMES to is large enough to hold strlen(from) char buf[100]= Old string ; char new[20]= Newer string ; strcpy(buf,new); printf( Variable buf contains: %s : and no more\n,buf); Variable buf contains: Newer string : and no more 19
strcat(char start[ ],char tail[ ]) Copies tail string at the end of start string ASSUMES start is large enough to hold both start and tail char start[100]= Beginning ; char end[20]= of a test. ; strcat(start,end); printf( Variable start contains: %s : and no more\n,start); Variable start contains: Beginning of a test. : and no more 20
strncat(char start[ ],char tail[ ],int n) Copies up to n bytes of tail string at the end of start string ASSUMES start is big enough to hold both start and n bytes of tail Safer than strcat char start[100]= Beginning ; char end[20]= of a test. ; strncat(start,end,6); printf( Variable start contains: %s : and no more\n,start); Variable start contains: Beginning of a t : and no more 21
strcmp(char a[ ], char b[ ]) Compares the string in a to the string in b If a<b, returns a number less than zero If a==b, returns zero If a>b, returns a number greater than zero Cannot compare strings with ==, <, >, <=, etc. operators! Can compare CHARACTERS with ==, <, if (0==strcmp(name, Tom )) printf( Hi Tom ); 22
strncmp(char a[], char b[],int n) Compares the string in a to the string in b for up to n characters If a<b, returns a number less than zero If a==b, returns zero If a>b, returns a number greater than zero Cannot compare strings with ==, <, >, <=, etc. operators! Can compare CHARACTERS with ==, <, if (0==strncmp(name, Tom,3)) printf( Name starts with Tom ); 23
Resources The C Programming Language, (K&R) Section 1.6 (Arrays), 1.9 (strings) Wikipedia C String Handling https://en.wikipedia.org/wiki/c_string_handling C String Tutorial : http://www.tutorialspoint.com/cprogramming/c_strings.htm 24