Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.

Similar presentations


Presentation on theme: "Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances."— Presentation transcript:

1 Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances of the same type of data. In C++ (and Java), an array is an indexed collection of data values of the same type. Arrays can represent collections of primitives or objects (e.g. int’s or SearchResult’s)

2 Fall 2004CS-183 Dr. Mark L. Hornick 2 Array Basics Recall that in Java, an array declaration merely creates a reference to an array, which must then be created via new int[] x; // int x[]; an alternate way x = new int[5]; // size 5 x int

3 Fall 2004CS-183 Dr. Mark L. Hornick 3 Java Arrays of objects Recall that a Java array of objects is actually an array of object references: Shape[] s; s = new Shape[5]; // size 5 s[0]= new Shape(Color, x1, x2, y1, y2); s shape

4 Fall 2004CS-183 Dr. Mark L. Hornick 4 C++ Array Basics In C++ the array size must be supplied as part of the definition; new is not used An array named x of type int can be defined in the following ways: int x[5];// array of 5 ints; uninitialized int x[5] = {3,2,6,4,1}; // initialized int x[] = {3,2,6,4,1}; // size implied x is really a pointer to the array (more later) int x

5 Fall 2004CS-183 Dr. Mark L. Hornick 5 Array Basics Declaration of arrays with constants is called fixed-size array declaration. Java is not limited to fixed-size array declaration. // Java array int n=12; int[] x;// x is an array name x = new int[n]; // size determined from variable n C++ is limited to fixed-size array declaration More restrictive than Java Better alternatives (like vector)

6 Fall 2004CS-183 Dr. Mark L. Hornick 6 C++ array limitations A Java array has a public constant length for the size of an array. for (i=0; i<x.length; i++)… In C++, there are no methods (e.g. length()) for determining the size of an array Shape x[5];// array of 5 shapes All C++ does is allocate a contiguous block of memory for the adjacent objects You have to know how big it is shape x

7 Fall 2004CS-183 Dr. Mark L. Hornick 7 Array indexing in C++ An indexed expression is used to refer to the individual values of the collection. C++ arrays use zero-based indexing (like Java) x[2]; // 3rd element There are no safeguards to prevent you from indexing to a location outside the bounds of the array. x[5]; // 6th element? shape x

8 Fall 2004CS-183 Dr. Mark L. Hornick 8 Pointers and Arrays The C++ variable representing an array maintains the location of the beginning of the memory block occupied by the array x (with no index) simply refers to the beginning of the array x is really just a pointer (to the first element of the array) int x[10]; // x points to the first of 10 elements int* ptr1 = x; // No type mismatch! ptr1 = &x[0]; // x contains the address of the 1 st element, // meaning x = &x[0]

9 Fall 2004CS-183 Dr. Mark L. Hornick 9 Pointers and Arrays Subscripting and dereferencing are similar Both convert a pointer to an object: int y = x[0]; // get first element y = *x; // same int* p = &x[0]; // or p = x y = *p; // get first element y = x[1]; // get second element y = *(x+1); // same y = *(p+1); // also the same

10 Fall 2004CS-183 Dr. Mark L. Hornick 10 Arrays of pointers int* py[5]; // array of 5 pointers // py is a pointer to an array of pointers Int* py 5 y

11 Fall 2004CS-183 Dr. Mark L. Hornick 11 char Arrays C/C++ have a special syntax for handling arrays of char’s An array of char’s is a C-Style string char msg[] = {‘H’,’e’,’l’,’l’,’o’,’\0’}; char msg[] = “Hello”; // more convenient Stored as: ‘H’,’e’,’l’,’l’,’o’,’\0’ char* msg = “Hello”; Last entry is ‘\0’, the null character

12 Fall 2004CS-183 Dr. Mark L. Hornick 12 Array example

13 Fall 2004CS-183 Dr. Mark L. Hornick 13 Passing arrays as arguments Since an array is just a pointer, you pass it as an argument as if you were passing a pointer Example

14 Fall 2004CS-183 Dr. Mark L. Hornick 14 Multi-dimensional Arrays An 2-dimensional array named x of type int can be defined in the following ways: int x[2][2];// 2x2 array of 4 ints int x[2][2] = {{3,2},{6,4}}; // initialized

15 Fall 2004CS-183 Dr. Mark L. Hornick 15 Program Arguments All C++ programs can have arguments By common convention In a console-mode program They come from the command line They are always passed to main int main(int argc, char* argv[]) Number of wordsArray of C-style strings

16 Fall 2004CS-183 Dr. Mark L. Hornick 16 Program Argument Example Command line myprog data.txt results.txt Arguments to main argc = 3 argv[0] = “myprog” argv[1] = “data.txt” argv[2] = “results.txt” m y p r o g \0 d a t a. t x t \0 r e s u l t s. t x t \0 argv


Download ppt "Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances."

Similar presentations


Ads by Google