Command Line Arguments / 2D Arrays

Download Report

Transcript Command Line Arguments / 2D Arrays

CGS 3460
Arrays of Arrays
(Multidimensional Arrays)


We have talked about arrays of ints, characters, floats,
etc.
In addition, you can have arrays of arrays
CGS 3460
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
ai , j
n columns
m
a
rows  0 , 0
i changes
 matrix[3][4];
 Not matrix[3,4], or matrix(3)(4)
m–by–n matrix
a
 1, 0
 a2 , 0

 
a0,1
a1,1
a2,1

j changes
a0, 2 

a1, 2 
a2, 2 

 
CGS 3460
Multidimensional Arrays

Initialization
 int M[2][3] = {{1, 2, 3}, {4, 5,6}};
M[0]
1
2
3
M[1]
4
5
6
CGS 3460
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]
1
2
3
M[1]
4
5
6
CGS 3460
Multidimensional Arrays

int M[2][3] = {[0][0] = 1,[1][2]=3};
M[0]
1
0
0
M[1]
0
0
3
CGS 3460
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
CGS 3460
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
CGS 3460
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
CGS 3460
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);
}
CGS 3460
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.
CGS 3460
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
CGS 3460
How to Interpret

A command-line interpreter
explains the command
 The first word is treated as the
name of a program
 Others as arguments.

cp
These strings are past to main
function in C
f1
f2
NULL
cp f1 f2
CGS 3460
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[] )
CGS 3460
How to use

argc
 Argument count
 Number of strings – including
executable file name

argv
 Argument vector
 Size of argc+1
cp
argv[0]
argv[1]
f1
argv[2]
f2
NULL
cp f1 f2