Something about programming

Arrays and strings in C language

Previous tutorial: Structures in C language
Next tutorial: Functions in C language

In this tutorial we'll learn how to work with arrays in C programming language. That will allow us to work with large amount of similar data. Also we'll learn how strings of text are represented in C language.

Arrays in C language

We already know how to work with variables of different data types in C language. But variables are work well for small number of objects - if we have one or two tanks in our game. But what if we'll have thousand of tanks? If we'll use only knowledge we already have, then we'll end up with these variables: tank1, tank2, tank3, ... tank999, tank1000. Of course that's inappropriate. C programming language gives us solution - arrays.

Array is a collection of variables of same type. Arrays can have different size. Each element in array have it's own place - elements are ordered and each element of array has index. We can get each element of array by it's index.

Let's start with simple example - we create the array of type int with 3 elements:

int numbers[3];

We must point the data type that we'll store in the array. Then there is identifier - name of the array. And in the square brackets we put number of elements in array. This line, as you may guess, reserves memory for array. It reserves 12 bytes (3 elements * 4 bytes of memory for int). In the memory this array could look something like this:

0x7fffb19c0cc0 // address of array and first element 0x7fffb19c0cc1 0x7fffb19c0cc2 0x7fffb19c0cc3 0x7fffb19c0cc4 // address of second element of array 0x7fffb19c0cc5 0x7fffb19c0cc6 0x7fffb19c0cc7 0x7fffb19c0cc8 // address of third element of array 0x7fffb19c0cc9 0x7fffb19c0cca 0x7fffb19c0ccb // array ends here

Indexes of arrays in C programming language

Each array element has it's own index. Indexes starts from zero: first element of array has index 0, second element - 1, third element - 2 and so on. We can work with each element like with separate variable:

numbers[0] = 1; numbers[1] = 324; numbers[2] = 899; printf("First element of array: %i\n", numbers[0]); // 1

As you can see we address elements of array with square brackets. Square brackets in C language have two meanings: when you define the array, square brackets are used to set number of elements in array (and reserve needed amount of memory); when you use element of array, in square brackets you put index of this element.

Address of array in C language

Array is just a chunk of memory that is divided to some number of elements. We already saw that using array identifier with square brackets and index allows us to get separate elements of array. But what if we'll try to use array identifier by itself? What array name means in C language? Here is the code:

int numbers[3]; numbers[0] = 1; printf("%p\n", numbers);

Last line will output the address. Array without square brackets is just the pointer. The address of array is the address of it's first element. We can think about indexes of array the other way. Index is the offset from the beginning of the array. 0 - beginning of array, 1 - offset by one element from the beginning...

Strings of text in C programming language

We know that C language has data type char that allows to store separate characters in variable. Characters should be put in single quotes. We know that string literals should be enclosed in double quotes. Now we need to bind these ideas together. Strings of text in C programming language are arrays of type char. Example:

char s[10] = "Hello"; printf("%c\n",s[0]); // H printf("%s\n",s); // Hello

Here we created the array with 10 elements and put in it the string with 5 letters. Then we output first letter (just to check what will happen) and at last we printed full string using format specifier %s - it prints strings of text. The thing is: our array should have 10 elements, but only 5 were printed, how is that? This happens because output stops when it meets zero. All string literals ends with zero. Strings in double quotes are called null-terminated strings in C language. We can check this:

char s[10] = "Hello"; if (s[5] == 0) { printf("Last element of string literal is zero\n"); }

So, our array actually contains 6 elements: letters of word Hello and zero. Also there are 4 empty (there can be garbage actually) elements of array. But we can just drop all elements of array (which contains characters) after we meet 0.

Conclusion

In this tutorial we learnt about arrays in C language. Also we learnt that strings are actually just arrays of type char.

Comments:

No comments yet