Author : Learntoprogram.com
Page : 1
Arrays are contiguous blocks of memory that hold many variables of the same type. Arrays can hold any type, but they must be of a fixed length (for example a float is fixed at 4 bytes). The size of an array is fixed when you declare it; you can't make it bigger or smaller, but you don't have to use all of it. It's very easy to write large arrays that take up a lot of memory, think about how big you need it to be when you declare it.
Declaring an array is not disimilar to declaring a normal variable. If you want an array of floats, for example, then the declaration would be
float myarray[50];
The square brackets here indicate that it is an array. The number in the square brackets indicates the size (or length) of the array.
The second way to declare an array is to define the values it has in it (and you can leave out the length if you want - the compiler will work it out), for example
float myarray[] = {1.0, 2.0, 3.0, 4.0, 5.0};
This declares and initialises an array with five elements in it. Alternatively
float myarray[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
Which explicitely declares the size of the array. Note that if you declare the values in an array you cannot leave out some values - you must define them all up to a point (the end of the array, some end point). You cannot for example have
float myarray [5] = {1.0, 2.0, , 3.0, 4.0};
You can have
float myarray[5] = {1.0, 2.0};
To access the elements of an array you use the name of the array and the number (properly called the index) of the element you would like to get at. For example
myarray[4] = 17.03;
The only tricky bit to accessing an element is remembering that arrays start from index 0 and go to one less than their length, eg
float myarray[2]; myarray[0] = 1.0;
myarray[1] = 2.0;
There is no element at myarray[2]. All the arrays in C++ work in this way and there is nothing you can do to change it. Be careful with the array indices because there won't be a warning if you do go over the end of an array. All that will happen is that later on in the program the bad value from off the end of the array will crash your program (sometimes and sometimes not). This can make very hard to find bugs in code that looks fine.
The maximum length of an array is 65535 elements. The maximum size of an array is not necessarily this big though. Some compilers limit array size to 64K bytes. Check your compiler's documentation to see if this is the case.
A string is a sequence of characters and in C++ it is coded as an array of characters. Most strings do not completely fill the array so there is another special character that is used at the end of the string. It is called null. Its symbol is '\0', that is the number zero, not the character O. The other way to refer to it is NULL.
String literals are enclosed in double quotes (characters are enclosed in single quotes). A string declaration with the variable assigned an initial value would look like
char string[] = "My Own String";
In this case there is no null character in here, but you could add one. Remember from arrays that this string will completely fill the character array. The other declaration for a string is just the same as a array declaration for a character array eg
char mystring[20];
There are however quite a few operations that we would like to do on strings. Fortunately many of them are provided in header files. The ones of most interest are:
Append using strcat; comparison using strcmp; duplication using strdup; and comparing two strings without taking account of the case of the letters using stricmp. All these string functions require null terminated strings (strings that end with '\0').
The strcmp function takes two strings and returns a pointer to the concatenated string. Its specification is
char *strcat(char *string1, const char *string2);
The const in the type declaration is simply there to ensure that the second string is not changed. The second string is added onto the back of the first string. The first string is changed and a pointer to it is returned by the function. This will make better sense after we look at pointers.
The strcmp function takes two strings and returns an integer. It does not change either of the strings. The function specification is:
int strcmp(const char *string1, const char *string2);
Its return value tells you how the two strings compared. If string1 is less than (comes lower in lexicographic order) then the return value is less than 0. At 0 the two strings are identical. If the return value is greater than 0 then string1 is greater than string2. Note that this function does regard the case of the letters, for example "one" is not equal to "One" is not equal to "OnE".
The stricmp function has the same functionality as the strcmp function, but it converts all the characters in the strings to lower case before it does its comparison.
The strdup function takes a single string as its argument and returns a copy of that string. This function allocates space for the string as it goes. If it fails to find the necessary space for the new string it returns NULL.
Not all of these functions are universal and many compilers provide extra string handling functions for you. Check your compiler's documentation for details.
Page : 1