class: center, middle ### COMP26020 Programming Languages and Paradigms Part 1: C Programming *** # Arrays, Strings, Command Line Arguments ??? - Hello Everyone. - In this video we are going to talk about arrays, strings and command line arguments in C --- # Arrays .leftcol[ ```c int array[4]; // declaration array[0] = 10; // write a slot array[1] = 20; array[2] = 30; array[3] = 40; int x = array[3]; // read a slot ``` ] .rightcol[ ```c printf("array[%d] contains %d\n", 0, array[0]); printf("array[%d] contains %d\n", 1, array[1]); printf("array[%d] contains %d\n", 2, array[2]); printf("array[%d] contains %d\n", 3, array[3]); // ``` .codelink[
`05-c-arrays-strings-cmdline/array.c`
] ] Arrays are stored contiguously in memory:
??? - Let's start with arrays. - They are declared with a statement starting with the type, followed by the name of the array variable, then between brackets the size of the array -- here 4 elements. - You can write something in an array slot with the affectation symbol, equals, and the index to write between bracket - For example here we write 10 in the first slot, 20 in the second one, and so on. - You'll notice that the indexing starts at 0, the first slot is index 0. - And you read a slot by indicating its index between brackets, for example here we read the fourth slot - Something important to note is that arrays are stored contiguously in memory. - So remember on x86-64 the size of int is 4 bytes. - We declared an array with 4 elements, so we have something like this in memory. - 16 contiguous bytes reserved by the compiler for the array, 4 bytes per slot. - Now if we execute this program, we'll see the content of the array printed. --- # Multi-Dimensional Arrays ```c int my_2d_array[3][2]; my_2d_array[0][0] = 0; my_2d_array[0][1] = 1; my_2d_array[1][0] = 2; my_2d_array[1][1] = 3; my_2d_array[2][0] = 4; my_2d_array[2][1] = 5; printf("my_2d_array[%d][%d] = %d\n", 0, 0, my_2d_array[0][0]); printf("my_2d_array[%d][%d] = %d\n", 0, 1, my_2d_array[0][1]); printf("my_2d_array[%d][%d] = %d\n", 1, 0, my_2d_array[1][0]); printf("my_2d_array[%d][%d] = %d\n", 1, 1, my_2d_array[1][1]); printf("my_2d_array[%d][%d] = %d\n", 2, 0, my_2d_array[2][0]); printf("my_2d_array[%d][%d] = %d\n", 2, 1, my_2d_array[2][1]); ``` .codelink[
`05-c-arrays-strings-cmdline/array-2d.c`
]
??? - Arrays can be of multiple dimensions. - To declare such an array you use several pairs of square brackets. - For example on the slide we can see the declaration of a 3 by 2 array. - We also refer to particular slots with pairs of square brackets. - This is for a 2 dimensional array but you can have as many dimensions as you want. - Arrays are still laid out contiguously in memory with the last dimension first, so we have array[0][0], followed by array[0][1], followed by array[1][0], and so on. - Executing this program prints the content of the array. --- # Arrays - Static initialisation allows to omit the size (of the first dimension) ```c int array[] = {1, 2, 3, 4, 5}; int array_2d[][2] = { {1, 2}, {3, 4}, {5, 6} }; printf("array[4] = %d\n", array[4]); printf("array_2d[2][0] = %d\n", array_2d[2][0]); ``` .codelink[
`05-c-arrays-strings-cmdline/array-init-static.c`
] ??? - Rather than initialising the array after its declaration, we can also initialise it with its declaration - This is called static initialisation - If you use it, you can omit the size of the first dimension, as shown on this example. - Here we create a one dimensional array with 5 slots, and a two dimensional array of size 3 by 2. - If we execute it we print array[4] which is 5 and array_2d[2][0] which is 5. --- # Character arrays, i.e. Strings - String in C: **array of characters** that ends with the special `\0` end of string character marker .leftcol[ ```c char string1[6] = "hello"; char string2[] = "hello"; char string3[6]; string3[0] = 'h'; string3[1] = 'e'; string3[2] = 'l'; string3[3] = 'l'; string3[4] = 'o'; string3[5] = '\0'; // Important here ``` ] .rightcol[ ```c printf("%s\n", string1); printf("%s\n", string2); printf("%s\n", string3); // ``` .codelink[
`05-c-arrays-string-cmdline/array-init-static.c`
] ]
??? - Let's talk bit about strings. - In C a string is simply a character array that is terminated with a special character, anti slash 0, marking the end of the string. - This character is used by many functions to know where a given string ends. - For example when we call printf on a string, it prints on the console all the characters of the array until it encounters the termination character. - We can declare it like an array. - It can be done statically, and we use the double quotes to indicate a string. - The compiler will automatically include the termination character for these. - However be careful to account for it when setting the size of the array, even if hello only has 5 characters, we need a sixth for the anti slash 0. - We can also fill an array after its declaration. - If we run this code, it prints 3 times hello. --- # Command Line Arguments
??? - Now that we know about arrays and strings we can see how to access command line arguments. - Command line arguments are strings passed on the command line when we invoke a program, separated with spaces - For example here we execute the program gcc with 3 arguments: test.c, dash o, and program --- # Command Line Arguments - `main` parameters: - `argc` - `argv`: array containing the command line arguments ```c int main(int argc, char **argv) { // 'char ** argv' means 'char argv[][]' printf("Number of command line arguments: %d\n", argc); // This is a bit dangerous... printf("argument 0: %s\n", argv[0]); printf("argument 1: %s\n", argv[1]); printf("argument 2: %s\n", argv[2]); return 0; } ``` .codelink[
`05-c-arrays-string-cmdline/cmdline-args.c`
] ??? - the main function can be declared with 2 parameters - argc, an integer indicating the number of command line arguments - and argv, a bi-dimensional character array, in other words an array of strings, that contains the arguments - Here is an example of a simple program simply printing the number of command line arguments and the first 3 of these arguments. - Don't be scared by the double star before argv, it is just another way to declare a 2 dimensional char array. - Let's try it out - I compile the program and call it with 3 arguments - You'll notice that the program name is the first member of the argument string array so for this particular invocation we don't see the last argument. - Finally, not that this program is not ideal: what happens if this code is called with less than 3 arguments - We get a memory error - This bug cannot be caught by the compiler - The program is still trying to print something but it's leaking weird values from the program memory - That's quite dangerous, there could be important things like a password in there - We'll remediate this in the next lecture regarding control flow --- # Command Line Arguments - Arguments are strings, to convert to integer use `atoi`: ```c #include
#include
// needed for atoi /* Sum the two integers passed as command line integers */ int main(int argc, char **argv) { int a, b; // Once again, dangerous! // We'll fix that in the next lecture regarding control flow a = atoi(argv[1]); b = atoi(argv[2]); printf("%d + %d = %d\n", a, b, a+b); return 0; } ``` .codelink[
`05-c-arrays-string-cmdline/atoi.c`
] - To convert a string to a `double` use `atof` ??? - Command line arguments are strings, so if you want to pass numbers you need to covert them. - You can do this with the atoi function. - To use it you need to include stdlib.h - Then you can pass a string as parameter and if successful atoi returns its conversion as an integer. - We can try this program that sums two integers passed as command line parameters. - To convert a string to a double you can call atof --- # Summary - Arrays - Strings - Command line arguments ---- .center[Feedback form: https://bit.ly/3xrfCAt]
??? - Let's recap. - In this video we talked about arrays - We also saw strings, that are nothing more than character arrays with a termination character - And we learn how to access command line parameters. - In the next video, we will talk about control flow, that is conditionals, and loops