Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multidimensional Arrays. Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order.

Similar presentations


Presentation on theme: "Multidimensional Arrays. Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order."— Presentation transcript:

1 Multidimensional Arrays

2 Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order

3 Arrays as arguments function X (int x, int y, int a [x] [y]) {... } Not valid in MANY C compilers New standard as of 1999 implemented ??? function X (int a[ ] [7]) {... } // valid highest dimension is still required

4 Dynamic arrays (1) Requires the use of the new operator Syntax: new Type [amount] int * ptr; // a ptr to an array of int's ptr= new int[5]; // note constant size if (dptr==NULL) … is better than if (dptr==0)…

5 Dynamic arrays (2) int y; cin>>y; int * ptr; // a ptr to an array of int's ptr= new int[y]; // note size determined at //run time BUT: the size of the array is still FIXED

6 Deleting storage Given the previous array: delete ptr; // deletes a SINGLE datum –the one memory loc that ptr refers to –Even if it's an array (only deletes array[0]) delete [ ] ptr; // deletes the whole array

7 Strings C-style strings (array of char) –char *x; x=new char [n] –Or Using namespace std; #include C++ style (true) strings #include –or using namespace std; #include


Download ppt "Multidimensional Arrays. Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order."

Similar presentations


Ads by Google