240-222 cpt: arrays of pointers/121 240-222 computer programming techniques semester 1, 1998...
TRANSCRIPT
240-222 CPT: Arrays of Pointers/12 1
240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998
Objectives of these slides:– to illustrate the use of arrays of pointers and
contrast them with 2D arrays
12. Arrays ofPointers
240-222 CPT: Arrays of Pointers/12 2
Overview:Overview:
1. An Array of Pointers
2. A 2D Array of Characters
3. An Array of char*
4. Sorting Words
5. Dynamic Memory Allocation
6. A Dynamic Array
7. Static versus Dynamic Memory
8. main() Arguments
240-222 CPT: Arrays of Pointers/12 3
1. An Array of Pointers1. An Array of Pointers
int *b[10];
int x[3], y[35];::
b[0] = x; b[1] = y;
240-222 CPT: Arrays of Pointers/12 4
2. A 2D Array of Characters2. A 2D Array of Characters
char names[][5] ={ "Bob", "Jo", "Ann", "Fred"};
printf("%s", names[0]);
‘B’ ‘o’ ‘b’ ‘\0’
‘J’ ‘o’ ‘\0’
‘A’ ‘n’ ‘n’ ‘\0’
‘F’ ‘r’ ‘e’ ‘d’ ‘\0’
names
240-222 CPT: Arrays of Pointers/12 5
3. An Array of char * Sec 7.9 / 7.83. An Array of char * Sec 7.9 / 7.8
char *names[] ={"Augustin", "Ann", "Bob", "Freddy"}
printf("%s", names[0]);
A u g u s t i n \0
A n n \0
B o b \0
F r e d d y \0
names[0]
names[1]
names[2]
names[3]
names
240-222 CPT: Arrays of Pointers/12 6
4. Sorting Words4. Sorting Words test.dat:
– A is for apple or alphabet pie which all get a slice of, come taste it and try.
Sort the words:$ sort_words < test.dat
Output:Aaallalphabet...which
240-222 CPT: Arrays of Pointers/12 7
sort_words.csort_words.c
#include <stdio.h>#include <stdlib.h>#include <string.h>
#define MAXWORD 50 /* max word length */#define SIZE 1000 /* array size */
void sort_words(char [][MAXWORD], int);void string_swap(char **, char **);
:
continued
240-222 CPT: Arrays of Pointers/12 8
int main(){ char w[SIZE][MAXWORD]; /* array of fixed length strings */ int num, i;
for (i=0; scanf("%s", w[i]) == 1; i++){ if (i >= SIZE) { printf("Too many Words!"); exit(1); } }
::
continued
240-222 CPT: Arrays of Pointers/12 9
: num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]);
return 0;}
240-222 CPT: Arrays of Pointers/12 10
5. Dynamic Memory Allocation5. Dynamic Memory Allocation
sort_words.c can save space by defining the size of each w[i] dynamically at run time.
New data structure:char *w[SIZE];
Make a pointer to a block of memory using:calloc(<num of elems>, <elem size>)
240-222 CPT: Arrays of Pointers/12 11
New Top LevelNew Top Level
#include <stdio.h>#include <stdlib.h>#include <string.h>
#define MAXWORD 50 /* max word length */#define SIZE 1000 /* array size */
void sort_words(char *[], int);void string_swap(char **, char **);
:
continued
240-222 CPT: Arrays of Pointers/12 12
int main(){ char *w[SIZE]; /* array of pointers */ char word[MAXWORD]; /* work space */ int num, i;
for (i=0; scanf("%s", word) == 1; i++){ if (i >= SIZE) { printf("Too many Words!"); exit(1); } w[i] = calloc(strlen(word)+1, sizeof(char)); strcpy(w[i], word); }
:continued
240-222 CPT: Arrays of Pointers/12 13
: num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]);
return 0;}
240-222 CPT: Arrays of Pointers/12 14
Some CommentsSome Comments
calloc(strlen(word)+1, sizeof(char))
cannot just use strcpy(w[i], word)
240-222 CPT: Arrays of Pointers/12 15
void sort_words(char *w[], int n)/* n elements are to be sorted *//* similar to bubble sort */{ int i, j;
for(i = 0; i < n; i++) for (j = i+1; j < n; j++) if (strcmp(w[i], w[j]) > 0) string_swap(&w[i], &w[j]);}
240-222 CPT: Arrays of Pointers/12 16
In picture form:In picture form:
A u g u s t i n \0
A n n \0
B o b \0
F r e d d y \0
w[0]
w[1]
w[2]
w[3]
w
240-222 CPT: Arrays of Pointers/12 17
void string_swap(char **p, char **q)/* swap the strings using pointers */{ char *temp;
temp = *p; *p = *q; *q = temp;}
240-222 CPT: Arrays of Pointers/12 18
In picture form:In picture form:
A u g u s t i n \0
A n n \0
w[0]
w[1]
A u g u s t i n \0
A n n \0
w[0]
w[1]
Before
After
240-222 CPT: Arrays of Pointers/12 19
6. A Dynamic Array6. A Dynamic Array
#include <stdio.h>#include <stdlib.h>
int main(){ int *a; /* will point to the array */ int i, size, sum = 0;
printf("An array will be created dynamically. Input an array size followed by values: ");
:
continued
240-222 CPT: Arrays of Pointers/12 20
scanf("%d", &size);
/* allocate space in a for size int's */ a = (char *)malloc(size * sizeof(int)); for (i=0; i < size; i++) scanf("%d", &a[i]);
for(i=0; i < size; i++) sum += a[i]; printf("Sum is %d\n", sum);
free(a); return 0;}
240-222 CPT: Arrays of Pointers/12 21
7. Static versus Dynamic Memory7. Static versus Dynamic Memory
The choice between static or dynamic memory is usually based on if you know how much data will be stored in your program.
If you know that an array of 100 integers is required then declare:
int data[100];
If you do not know, then consider creating dynamic memory.
240-222 CPT: Arrays of Pointers/12 22
8. 8. main()main() Arguments Arguments
/* my_echo.c: Echo command line input */#include <stdio.h>
int main(int argc, char *argv[]){ int i;
printf("argc = %d\n", argc); for (i = 0; i <= argc-1; ++1) printf("argv[%d] = %s\n", i, argv[i]); return 0;}
240-222 CPT: Arrays of Pointers/12 23
Compilation and Execution:Compilation and Execution:
$ gcc -Wall -o my_echo my_echo.c
$ my_echo a is for apple
argc = 5argv[0] = my_echoargv[1] = aargv[2] = isargv[3] = forargv[4] = apple