Basic Arrays

Chris Wild & Steven Zeil

Last modified: Jun 25, 2014

Contents:
1. Description
2. Example
3. Tips
4. String Literals
4.1 Description
4.2 Example
4.3 Tips
5. Common Errors Using Arrays

1. Description

2. Example

int someArray[3]; // this is an array of three integers.
someArray[0] = 567; // set the first element in the array 
                    //   to the integer constant "567";
int someInteger = 4; // this is NOT an array - just a plain 
                     //   integer with initial value "4"
someArray[1] = someInteger; // sets the second element in the
                            //   array to the value "4"
// At this point the value of the third element of the 
//  array (someArray[2]) is undefined

float aVector[100]; // a vector of 100 floating point
                    //   numbers, indexed from 0 to 99
aVector[99] = 3.4; // sets the value of the last element to 3.4

char aString[20]; // an array of characters is also known as a string
aString[9] = 'z'; // sets the 10th character to 'z'

3. Tips

4. String Literals

4.1 Description

character desired escape sequence description
" " double quote
\ \\ backslash
null character \0 Used as null termination character
tab \t tab character
new line \n new line character

4.2 Example


char name[4] = "Pam"; // need 4 characters - don't forget the 
                      //  null termination character implicit at
                      //  the end of a string literal
name[0] = 'S'; // changes name to "Sam"
cout << name; // prints out the string "Sam"

char anotherName[6]; // unused array
anotherName[0] = name[0]; // copies the character 'S' to anotherName
anotherName[1] = name[1]; // copies the character 'a' to anotherName
anotherName[2] = name[2]; // copies the character 'm' to anotherName
anotherName[3] = name[3]; // copies the character '\0' (the null 
                          //   termination character)  to anotherName
cout << anotherName; // prints out the string "Sam"
cin >> name; // reads character up to the first
             //    white space, puts in name and adds null termination character
//// NOTE: assumes user types less than 4 characters - otherwise
//        will overflow the array and lead to possible programming errors.
//// This is one of the reasons to avoid character arrays and to use
//     the  "string" class.

4.3 Tips

5. Common Errors Using Arrays

Example: Common Errors


// the following statements are not allowed and will cause compiler errors 

int a[10], b[10];

a = b; // cannot assign an entire array - visual c++ gives left operand not a l-value
a[3] = b[3]; // this is OK

int [ ] myFunction( ); // cannot return an entire array
int* myFunction( ); // this is OK

Example: Errors in Reading Arrays

// Examples in reading input
   char someString[11]; // holds up to 10 characters plus the null character

   cin >> someString; // reads next bunch of non-whitespace characters into somestring
                      // inserts null character are end
                      // assumes that this will be 10 or less characters

   cin.getline(someString,11); // reads at most 10 characters or until 
                               //   end of line
        // inserts null character at end
        // if the line contains 9 or less characters, the new line 
        //    character is removed
        // if the line contains more than 9 characters, the extra
        //    characters (if any) and the newline are kept.

   cin.get(someString,11);  // reads at most 10 characters or until end of line
        // inserts null character at end
        // Unlike getline above, the new line character is never removed.

   cin.get(someString,11,'#'); // reads at most 10 characters or until
                               //   the character '#' is found


// to handle getting one line of input, even if it is too long
// ignoring the extra characters (if any)
   cin.get(someString, 11);
   cin.ignore(200,'\n'); // ignore up to 200 characters but stop at 
                         //    the first newline is less than 200
              // assumes that there will be less than 200 characters
              //    extra on this line.