L11 - arrays and strings

L11 - arrays and strings - C-style Arrays in C An array is...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
C-style Arrays in C++ An array is a structured collection of components all of the same data type – The structured collection is given a single name – The data in the collection are called array elements – The elements are stored in adjacent memory locations Individual elements are accessed by using the array name together with an integer-valued index in square brackets, e.g. points[3] The index tells the position of the component within the collection – and indexing starts with index 0, not 1 There are two categories of arrays in C++ – static arrays – those for which the compiler determines how memory is allocated – dynamic arrays – those for which memory allocation is done at runtime
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaring and Referencing Static Arrays • To declare an array: <type-name> <array-name> [<declared-size>]; Examples: int big_array[100]; double data_points[1000]; char grades[5]; To assign a value to the first element of the array data_points, we would write data_points[0] = 1.2324; To get the value of the last element in the array grades, we would write gd = grades[4];
Background image of page 2
Important Observations: The size of a standard static C++ array cannot be changed once the array is declared An array name, in most cases, is treated as a pointer to the first element of the array Arrays are stored in consecutive memory locations – each element of the array occupies as much space as the base type of the array: An array can have any data type: int, double, bool, char, etc. How much room an array takes depends on its base type: • if it is an int array (each int takes 4 bytes) then the array is 4 * # elements long • if it is a double array (each double takes 8 bytes) then the array is 8 * # elements long • if it is a char array (each char takes 1 byte) then the array is 1 * # elements long x[0] 22 X[1] 13 X[2] 5 X[n-1] 75
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
a C-string is an array of characters that ends in the null character \0 char str[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘\0’}; char grades[6] = {"ABCDF"}; char vowels[] = {"aeiouy"}; If you work with C-strings, there is a large library of functions in the standard C++ library that you can use: – strcpy, strcmp, strcat, strlen, etc. – these string functions look for the null character to find the end of the string – You usually have to #include <cstring> to have access to these library functions, but … – In the .NET framework, if you are using namespace std the functions are available to you without having to include the cstring header If we use an array of characters that is NOT a C-string (i.e. is not null-terminated), what do you think will happen? – it depends on what you are doing and how you are doing it
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course EE 361 taught by Professor Conry during the Spring '08 term at Clarkson University .

Page1 / 25

L11 - arrays and strings - C-style Arrays in C An array is...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online