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
26
Embed
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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.
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]
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); }
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)
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.
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]
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.
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}}
};
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?
Arrays and Pointers 11
Arrays of Pointers: 2-D arrays contain the same number of elements in each row. For example:
Space is wasted in the rows containing shorter strings. Can we build an array whose rows can vary in length?
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.
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:
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,
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); }
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
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); }
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”
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
Arrays and Pointers 20
Self-Test: char s1[] = “beautiful big sky country”,
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;
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;
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 */
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 */