Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements...

26
Arrays and Pointers 1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain a two-dimensional array. To obtain arrays of higher dimension, we simply continue to add brackets. int a[100]; // an one-dimensional array int b[2][7]; // a two-dimensional array int c[5][4][3]; // a three-dimensional array

Transcript of Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements...

Page 1: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 1

Arrays of Arrays:We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain a two-dimensional array. To obtain arrays of higher dimension, we simply continue to add brackets.

int a[100]; // an one-dimensional array int b[2][7]; // a two-dimensional array int c[5][4][3]; // a three-dimensional array

Page 2: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 2

Size of a Multi-dimensional Array:

A k-dimensional array has a size for each of its k dimensions. Let si represent the size of the array’s ith dimension, such as

int a[s1][s2]…[sk]; then the declaration of the array a

allocates space for s1 s2 … sk elements.

Page 3: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 3

Example: int a[3][5];

Col 1 Col 2 Col 3 Col 4 Col 5

Row 1 a[0][0]

a[0][1]

a[0][2]

a[0][3]

a[0][4]

Row 2 a[1][0]

a[1][1]

a[1][2]

a[1][3]

a[1][4]

Row 3 a[2][0]

a[2][1]

a[2][2]

a[2][3]

a[2][4]

Page 4: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 4

#define ROWS 3#define COLS 4int main(){

int a[ROWS][COLS], i, j, sum = 0;for (i = 0; i < ROWS; ++i) /* fill the array */

for (j = 0; j < COLS; ++j) a[i][j] = i + j;

for (i = 0; i < ROWS; ++i){ /* print the array */for (j = 0; j < COLS; ++j) printf(“a[%d][%d] = %d ”, i, j, a[i][j]);printf(“\n”);

}for (i = 0; i < ROWS; ++i) /* sum the array */

for (j = 0; j < COLS; ++j) sum += a[i][j];

printf(“\nsum = %d\n”, sum); }

Page 5: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 5

Relationship between pointers and arrays:

int a[3][5];

Expressions equivalent to a[i][j]

*(a[i] + j)

(*(a + i))[j]

*(*(a + i) + j)

*(&a[0][0] + 5*i + j)

Page 6: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 6

Storage Mapping Function:

For any array, the mapping between pointer values and array indices is called the storage mapping function. For example:

int a[ROWS][COLS]; a[i][j] *(&a[0][0] + i*COLS + j) Note: the array name a by itself is

equivalent to &a[0], a pointer to an array of COLS ints. The base address of the array is &a[0][0], not a.

Page 7: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 7

Multi-dimensional Arrays as Formal Parameters:

In a function definition, a multi-dimensional array argument must be specified by all sizes except the first dimension.

int sum(int a[][5])int i, j, sum = 0;for (i = 0; i < ROWS; ++i) for (j = 0; j < 5; ++j) sum += a[i][j];return sum;

}Note: the following parameter declarations are equivalent:

int a[][5] int (*a)[5] int a[3][5]

Page 8: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 8

Initialization:The following three initializations are equivalent. int a[2][3] = {1, 2, 3, 4, 5, 6}; int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int a[][3] = {{1, 2, 3}, {4, 5, 6}}; If there are no inner braces, then each array elements is initialized in turn (row-wise). If there are fewer values than elements in the array, then the remaining elements are initialized to zero. If the first bracket pair is empty, then the compiler takes the size from the number of inner brace pairs. All sizes except the first must be given explicitly.

Page 9: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 9

Another Example: int a[2][2][3] = {

{{1, 1, 0}, {2, 0, 0}},{{3, 0, 0}, {4, 4, 0}}

}; /* or equivalently */ int a[][2][3] = {

{{1, 1}, {2}}, {{3}, {4, 4}}

};

Page 10: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 10

Dynamic Allocation:

Suppose we want to build a student name list. The program must read its input from keyboard (by calling getline function) and store the input into some kind of table. What king of table? If we use an array of pointers, how to allocate space for each entry?

Page 11: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 11

Arrays of Pointers: 2-D arrays contain the same number of elements in each row. For example:

char days[][10] = {{‘m’,’o’,’n’,’d’,’a’,’y’,’\0’},{‘t’,’u’,’e’,’s’,’d’,’a’,’y’,’\0’},…

};

Space is wasted in the rows containing shorter strings. Can we build an array whose rows can vary in length?

Page 12: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 12

Ragged Array: A ragged array is an array of pointers where each entry in the array is a pointer to a string (arbitrary length). For example:char *days[] = { “monday”, “tuesday”, “wednesday”, “thursday”, “friday”, “saturday”, “sunday” };

The compiler allocates an array containing 7 elements and assigns each element a pointer to the corresponding string.

Page 13: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 13

m o n d a y \0

t u e s d a y \0

w e d n e s d a y \0

t h u r s d a y \0

f r i d a y \0

s a t u r d a y \0

s u n d a y \0

2-D Array:

*

*

*

*

*

*

*

m o n d a y \0

t u e s d a y \0

s u n d a y \0

……

Array of pointers:

Page 14: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 14

Accessing Elements: Elements in a ragged array can be access in either pointer-based access or array-based access. For example,

char *days[] = { “monday”, “tuesday”, “wednesday”, “thursday”, “friday”, “saturday”, “sunday” };

Suppose we want to access the third char of “wednesday”, the following are equivalent:

days[2][2] *(days[2] + 2) *(*(days + 2) + 2) days is the base-address of the array of pointers days[k] is the pointer to the kth string

Page 15: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 15

Self-Test:char *days[] = { “monday”, “tuesday”,

“wednesday”, “thursday”, “friday”, “saturday”, “sunday” };

Expression:

*(days[2] + 4)

days[3][5]

days[4]

*(days[5])

days[6][6]

**days

Value:

‘e’

‘d’

“friday”

‘s’

‘\0’

‘m’

Page 16: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 16

Traversing an Array of pointers:#include <stdio.h> /* array-based (index) */ void printStrs1(char *s[], int n){

int i;for (i = 0; i < n; i++)

printf(“%s\n”, s[i]);

} /* pointer-based */ void printStrs2(char **sp, int n){

char ** ep = sp + n;for ( ; sp < ep; sp++)

printf(“%s\n”, *sp); }

Page 17: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 17

#include <stdio.h>

#include <string.h> #include <stddef.h> #include <stdlib.h> #define MAXLEN 80 #define MAXNAMES 100 static char *newStr(char *str){ // a static function will be handled as an inline function

char * newstr = malloc(strlen(str) + 1);if (newstr != NULL) strcpy(newstr, str);return newstr;

} int main(){

int k;char *nameTable[MAXNAMES], name[MAXLEN + 1], *sp;for (k = 0; getline(name, MAXLEN) != -1 && k < MAXAMES; k++){

if ((sp = newStr(name)) != NULL)nameTable[k] =sp;

else {printf(“Memory run out\n”);break;

} }

printStr(nameTable, k); }

Page 18: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 18

Command-line Arguments: When we run a program, we often provide some initial values the program will work with. For Example:

$myprogram filename 222 Two arguments, argc and argv, can be used with the main() to communicate with the operating system, where argc is the number of arguments on the command-line, and argv is an array of pointers to strings containing the various arguments. For the above example:argc == 3argv[0] == “myprogram”argv[1] == “filename”argv[2] == “222”

Page 19: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 19

Passing Arguments to main():#include <stdio.h>int main(int argc, char *argv[]){

int k;printf(“argc = %d\n”, argc);for (k = 0; k < argc; k++)

printf(“argv[%d] = %s\n”, k, argv[k]);}

$ mytest –o try this argc = 4 argv[0] = mytest argv[1] = -o argv[2] = try argv[3] = this

Page 20: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 20

Self-Test: char s1[] = “beautiful big sky country”,

s2[] = “how now brown cow”;

Expression:

strlen(s1)

strlen(s2 + 8)

strcmp(s1, s2) Statement:

printf(“%s”, s1 + 10);

strcpy(s1 + 10, s2 + 8);

strcat(s1, “s!”);

printf(“%s”, s1);

Value:

25

9

negative int

what is printed

big sky country

beautiful brown cows!

Page 21: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 21

Self-Test: char *p[2][3] = {{“abc”, “defg”, “hi”},

{““jklmno”, “pqrstuvw”, “xyz”}};

Expression: Equivalent expression:

***p p[0][0][0]

**p[1]

**(p[1] + 2)

*(*(p + 1) + 1)[7]

(*(*(p + 1) + 1))[7]

*(p[1][2] + 2)

Value:

‘a’

‘j’

‘x’

error

‘w’

‘z’

Page 22: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 22

A Union and Structure Example:typedef enum {AUTO, BOAT, PLANE, SHIP} VTYPE;

typedef struct { /* structure for an automobile */ int tires; int fenders; int doors;

} AUTOTYPE;

typedef struct { /* structure for a boat or ship */ int displacement; char length;

} BOATTYPE;

Page 23: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 23

typedef struct { char engines; int wingspan;

} PLANETYPE;

typedef struct { VTYPE vehicle; /* what type of vehicle? */ int weight; /* gross weight of vehicle */ union { /* type-dependent data */

AUTOTYPE car; /* part 1 of the union */ BOATTYPE boat; /* part 2 of the union */PLANETYPE airplane; /* part 3 of the union */ BOATTYPE ship; /* part 4 of the union */

} vehicle_type; int value; /* value of vehicle in dollars */ char owner[32]; /* owners name */

} VEHICLE;

Page 24: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 24

void print_auto(VEHICLE * pv){printf(“Auto weight = %d\n”, pv->weight);printf(“# of tires: %d\n, # of fenders: %d\n, # of doors: %d\n”, pv->vehicle_type.car.tires, pv->vehicle_type.car.fenders, pv->vehicle_type.car.doors); printf(“Cost = $%d\n”, pv->value); printf(“Owned by %s\n”, pv->owner); /* owners name */

}

void print_plane(VEHICLE * pv){ …}; void print_boat(VEHICLE * pv){ …}; void print_ship(VEHICLE * pv){ …};

Page 25: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 25

void print_vehicle(VEHICLE * pv){switch (pv->vehicle){

case AUTO: print_auto(pv);break;

case PLANE: print_plane(pv);break;

case BOAT: print_boat(pv);break;

case SHIP: print_ship(pv);break;

default: printf(“Unknown vehicle type\n”);}

}

Page 26: Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.

Arrays and Pointers 26

void main() {VEHICLE v[2], *piper_cub;

v[0].vehicle = AUTO; v[0].weight = 2742; /* with a full gas tank */ v[0].vehicle_type.car.tires = 5; /* including the spare */ v[0]. v[0].vehicle_type.car.doors = 2; /* … other initialization of v[0] */v[1].vehicle = BOAT;v[1].value = 3742; /* trailer not included */ v[1].vehicle_type.boat.length = 20; /* … other initialization of v[1] */piper_cub = (vehicle*) malloc(sizeof(VEHICLE));piper_cub->vehicle = PLANE; piper_cub->vehicle_type.airplane.wingspan = 27; /* … other initialization of piper_cub */print_vehicle(v);print_vehicle(&v[1]);print_vehicle(piper_cub); /* piper_cub is a pointer to VEHICLE */

}