27
Lecture 9 1 Chapter 9 Multidimensional Arrays and Double Indirection

Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 1

Chapter 9

Multidimensional Arrays and

Double Indirection

Page 2: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 2

Topics

• Two Dimensional Arrays

• Storage Class and Scope of Multi-

Dimensional Arrays

• Arrays with more than 2 dimensions

• Arrays of Pointers

• Double Indirection

• Command Line Parameters

Page 3: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 3

Two Dimensional Arrays

• Up till now, all arrays were considered

to be one dimensional.

• Each array had a set of values that

were associated with a single index.

Page 4: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 4

Two Dimensional Arrays

• A two-dimensional array still has only

one value per cell, but the location of

the cell is handled by two indexes

• It is sometimes handy to think of a two

dimensional array as being a set of

rows and columns of a table.

Page 5: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 5

Two Dimensional Arrays

Col 0 Col 1 Col 2

Row 0 6.1 1.2 8.3

Row 1 7.4 5.5 3.6

Row 2 2.7 9.8 4.9

Page 6: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 6

Two Dimensional Arrays

• datatype identifier[first limit][second limit];

• float two_d_float[2][3];

• float two_d_float[3][2];

• int two_d_int[4][2];

• char two_d_char[3][5];

Page 7: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 7

Two Dimensional Arrays

• The contents of the cell of the array can be accessed.

• Given:

• float Table15[3][3]

• Table15[0][0]

• 6.1

• Table15[1][1]

• 5.5

• Table15[2][2]

• 4.9

Page 8: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 8

Storage Class and Scope of

Multidimensional Arrays

• Multidimensional arrays have the same

behavior as earlier datatypes.

• The default is automatic

• They can be declared static

• They can be local, global, or external

global.

Page 9: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 9

Storage Class and Scope of

Multidimensional Arrays

• 2-dimensional arrays can be assigned

at the time of declaration

static float two_d[3][3]={{1.1,2.3,3.5},

{5.8,8.13,13.21},

{21.34,34.55,55.89}

};

Page 10: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 10

Multidimensional Arrays as Parameters

to Functions

• If a two-dimensional array is passed to

a function, it is not the same as a one-

dimensional array.

• A two dimensional array has to have

at least one of its dimensions defined.

Page 11: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 11

Multidimensional Arrays as Parameters

to Functions void TwoDimFunction1(float dataset[][COL],int m)

{

int i,j;

for (j=0;j<COL; ;j++)

{

{

for (i=0;i<m;i++)

printf("%g ",dataset[i][j]);

}

printf("\n");

}

}

#define COL 3

void main()

{

float dataarray[3][3]={{1.0,2.0,3.0},

{4.0,5.0,6.0},

{7.0,8.0,9.0}};

TwoDimFunction1(dataarray, 2);

while('q'!=getchar());

}

Page 12: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 12

Multidimensional Arrays as Parameters

to Functions void TwoDimFunction2(float (*dataset) [3],int m,int

n)

{

int i,j;

for (i=0;i<m;i++)

{

for (j=0;j<n;j++)

{

printf("%g ",dataset[i][j]);

}

printf("\n");

}

}

void main()

{

float dataarray[3][3]={{1.0,2.0,3.0},

{4.0,5.0,6.0},

{7.0,8.0,9.0}};

TwoDimFunction1(dataarray,3,3);

while('q'!=getchar());

}

Page 13: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 13

Arrays with more than two dimensions

• A multidimensional array is not a

representation of the physical

dimensions.

• An array can have three dimensions,

four dimensions, five dimensions,

depending on how many indexes of

information are used to identify a cell.

Page 14: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 15

Arrays with more than two dimensions

float dataarray1[2][2][2]=

{

{

{1,2},

{3,4}

},

{

{5,6},

{7,8}

}

};

void ThreeDimFunction1(float dataset[][2][2],int m,int n,int p)

{

int i,j,k;

for (i=0;i<m;i++)

{

for (j=0;j<n;j++)

{

for (k=0;k<p;k++)

{

printf("%g\n",dataset[i][j][k]);

}

}

}

}

Page 15: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 16

0,0,0 0,1 0

1 0,0 1,1,0

Slice 0

Slice n

int data[2][2][n];

0,0,1 0,1 1

1 0,1 1,1,1

Slice 1

Page 16: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 17

Arrays with more than two dimensions

float dataarray1[2][2][2]=

{

{

{1,2},

{3,4}

}, {

{5,6},

{7,8}

} };

void ThreeDimFunction2(float (*dataset)[2][2],int m,int n, int p)

{

int i,j,k;

for(i=0;i<m;i++)

for (j=0;j<n;j++)

for(k=0;k<p;k++)

{

printf("%g\n",dataset[i][j][k]);

}

}

Page 17: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 18

Arrays of Pointers

• Its is possible to

make an array of

pointers

• char *ptrarray[4];

ptrarray

Page 18: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 19

Double Indirection

• Any variable in C can have a pointer

to it.

• A pointer is a variable in C

• Therefore, a pointer can point to

another pointer

Page 19: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 20

Double Indirection

• int **ptr; is a pointer to a pointer.

• Also,

int ***ptr;

int *****ptr;

int ******ptr;

Page 20: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 21

Command Line Parameters

• The main function of a C program is

like any other function

• The main has a return type

• The main actually has a parameter list

Page 21: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 22

Command Line Parameters

int main(int argc, char *argv[])

{

/*Code Here*/

}

Page 22: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 23

Command line parameters

• The default return type of the main

function is an int

• This value usually comes from the

exit(n) command

• The default return value of the main

function is a 0.

Page 23: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 24

Command line parameters

• The default parameter list for the main

function has two types, an int, and a

two dimensional array of char.

Page 24: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 25

Command line parameters

• The int parameter

• usually named with the identifier argc,

• for “argument count”

• is a count of how many parameters are

called in the main plus one.

Page 25: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 26

Command line parameters

C:\MyProgram A B C D What is the value of

argc?

5

Page 26: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 27

Command line parameters

• The two-dimensional array of characters is actually an array of strings

• usually named with the identifier argv, for “argument vector”

• The command line is parsed by the blank space to make each entry of the argument vector, including the name of the program.

Page 27: Chapter 9 Multidimensional Arrays and Double Indirectionranger.uta.edu/~iahmad/lectures1311/lecture9.pdf · Lecture 9 13 Arrays with more than two dimensions ... strings •usually

Lecture 9 28

Command line parameters

C:\MyProgram A B C D

What is the value of argv?

argv[0]= “MyProgram”

argv[1]= “A”

argv[2]= “B”

argv[3]= “C”

argv[4]= “D”