1 / 14

Arrays of Arrays (Multidimensional Arrays)

Arrays of Arrays (Multidimensional Arrays). We have talked about arrays of ints, characters, floats, etc. In addition, you can have arrays of arrays. Multidimensional Arrays. Declaration type name[ mrows][ncols ]; e.g. int matrix[5][10]; What it means? Declare an array of size mrows

sidney
Download Presentation

Arrays of Arrays (Multidimensional Arrays)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays of Arrays(Multidimensional Arrays) • We have talked about arrays of ints, characters, floats, etc. • In addition, you can have arrays of arrays

  2. Multidimensional Arrays • Declaration • type name[ mrows][ncols ]; • e.g. int matrix[5][10]; • What it means? • Declare an array of size mrows • Each element of that array will be another array of size ncols of the given typ • Access • matrix[3][4]; • Not matrix[3,4], or matrix(3)(4) m–by–n matrix j changes n columns m rows i changes

  3. Multidimensional Arrays • Initialization • int M[2][3] = {{1, 2, 3}, {4, 5,6}}; M[0] M[1]

  4. Multidimensional Arrays • int M[2][3] = {1, 2, 3, 4, 5, 6}; • Numbers will fill up the first row, then the second row,… M[0] M[1]

  5. Multidimensional Arrays • int M[2][3] = {[0][0] = 1,[1][2]=3}; M[0] M[1]

  6. Arrays and Pointers • Recall arrays are pointers • int a[10]; • a is a pointer to a location in memory where space for 10 ints is reserved • int b[5][20] • b is an array of arrays • b[0] gives and array of ints of size 20 • Since arrays are pointers • b is also an array of pointers • b can also be considered as a pointer to a location in memory where space for 5 integer pointers is reserved • b is a pointer to a pointer to an integer

  7. Dynamic Allocation of 2D arrays • Remember malloc • malloc(sizeof(int) * 15) would reserve space for 15 ints and return the address of the first int • What would malloc(sizeof(int *) * 10) do? • reserve space for 15 integer pointers and return the address of the first int

  8. Dynamic allocation of 2 D Arrays • Want to declare an integer array with M rows and N columns called A. • What is A’s type? • int ** A; • What should A point to? • Array of integer pointers (int *) • A = malloc(sizeof(int*) * M) • A[i] is a different pointer to an int for every I • What should A[i] point to? • Array of integers (int) • A[i] = malloc(sizeof(int) * N) for all i • A[i][j] gives you an int at the i, j index

  9. Summary int** A; int M, N, i, j; A = malloc(sizeof(int*) * M); for(i = 0; i < M; i++) { A[i] = malloc(sizeof(int) * N); }

  10. Two dimensional character arrays • char A[5][100] • Contains 5 arrays of 100 characters • Could contain 5 arrays of strings (with max size 99) • A[0]  “foo” • A[1]  “bar” • etc.

  11. Command line arguments • Command-line arguments • Given after the name of a program in command-line • cp f1 f2 • gcc yourfilename • Arguments are passed in to the program from the operating system • Flexible • But not required

  12. How to Interpret • A command-line interpreter explains the command • The first word is treated as the name of a program • Others as arguments. • These strings are past to main function in C cp f1 f2 NULL cp f1 f2

  13. How to Declare • In C, these strings are past to the main function • main() can actually accept two arguments • number of command line arguments • a full list of all of the command line arguments. • Declaration • int main ( int argc, char *argv[] )

  14. How to use • argc • Argument count • Number of strings – including executable file name • argv • Argument vector • Size of argc+1 cp f1 f2 NULL cp f1 f2

More Related