Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-222 CPT: Arrays of Pointers/121 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.

Similar presentations


Presentation on theme: "240-222 CPT: Arrays of Pointers/121 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays."— Presentation transcript:

1 240-222 CPT: Arrays of Pointers/121 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays of pointers and contrast them with 2D arrays 12. Arrays of Pointers

2 240-222 CPT: Arrays of Pointers/122 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

3 240-222 CPT: Arrays of Pointers/123 1. An Array of Pointers int *b[10]; int x[3], y[35]; : : b[0] = x;b[1] = y;

4 240-222 CPT: Arrays of Pointers/124 2. 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’ ‘\0’ ‘F’‘r’‘e’‘d’‘\0’ names

5 240-222 CPT: Arrays of Pointers/125 3. An Array of char * Sec 7.9 / 7.8 char *names[] = {"Augustin", "Ann", "Bob", "Freddy"} printf("%s", names[0]); A ugustin\0 Ann Bob Freddy names[0] names[1] names[2] names[3] names

6 240-222 CPT: Arrays of Pointers/126 4. Sorting Words test.dat : –A is for apple or alphabet pie which all get a slice of, come taste it and try. l Sort the words: $ sort_words < test.dat l Output: A a all alphabet... which

7 240-222 CPT: Arrays of Pointers/127 sort_words.c #include #include #include #define MAXWORD 50 /* max word length */ #define SIZE 1000 /* array size */ void sort_words(char [][MAXWORD], int); void string_swap(char **, char **); : continued

8 240-222 CPT: Arrays of Pointers/128 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

9 240-222 CPT: Arrays of Pointers/129 : num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]); return 0; }

10 240-222 CPT: Arrays of Pointers/1210 5. Dynamic Memory Allocation sort_words.c can save space by defining the size of each w[i] dynamically at run time. l New data structure: char *w[SIZE]; l Make a pointer to a block of memory using: calloc(, )

11 240-222 CPT: Arrays of Pointers/1211 New Top Level #include #include #include #define MAXWORD 50 /* max word length */ #define SIZE 1000 /* array size */ void sort_words(char *[], int); void string_swap(char **, char **); : continued

12 240-222 CPT: Arrays of Pointers/1212 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

13 240-222 CPT: Arrays of Pointers/1213 : num = i; sort_words(w, num); for (i=0; i < num; i++) printf("%s\n", w[i]); return 0; }

14 240-222 CPT: Arrays of Pointers/1214 Some Comments calloc(strlen(word)+1, sizeof(char)) cannot just use strcpy(w[i], word)

15 240-222 CPT: Arrays of Pointers/1215 void sort_words(char *w[], int n) /* n elements are to be sorted */ /* similar to bubble sort */ { int i, j; for(i = 0; i 0) string_swap(&w[i], &w[j]); }

16 240-222 CPT: Arrays of Pointers/1216 In picture form: Augustin\0 Ann Bob Freddy w[0] w[1] w[2] w[3] w

17 240-222 CPT: Arrays of Pointers/1217 void string_swap(char **p, char **q) /* swap the strings using pointers */ { char *temp; temp = *p; *p = *q; *q = temp; }

18 240-222 CPT: Arrays of Pointers/1218 In picture form: Augustin\0 Ann w[0] w[1] Augustin\0 Ann w[0] w[1] Before After

19 240-222 CPT: Arrays of Pointers/1219 6. A Dynamic Array #include #include 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

20 240-222 CPT: Arrays of Pointers/1220 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; }

21 240-222 CPT: Arrays of Pointers/1221 7. Static versus Dynamic Memory l The choice between static or dynamic memory is usually based on if you know how much data will be stored in your program. l If you know that an array of 100 integers is required then declare: int data[100]; l If you do not know, then consider creating dynamic memory.

22 240-222 CPT: Arrays of Pointers/1222 8. main() Arguments /* my_echo.c: Echo command line input */ #include 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; }

23 240-222 CPT: Arrays of Pointers/1223 Compilation and Execution: $ gcc -Wall -o my_echo my_echo.c $ my_echo a is for apple argc = 5 argv[0] = my_echo argv[1] = a argv[2] = is argv[3] = for argv[4] = apple

24 240-222 CPT: Arrays of Pointers/1224 Pictorially $ my_echo hello, world produces: my_echo\0 world argv[0] argv[1] argv[2] argv[3] hello,\0 NULL argv


Download ppt "240-222 CPT: Arrays of Pointers/121 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays."

Similar presentations


Ads by Google