Script started on Wed Oct 30 12:31:09 2024 mweeks@air:C_ch16$ echo code is 4392 code is 4392 mweeks@air:C_ch16$ cat C_ch16_11.c /* See Kim King's book, page 403. This is an expanded example with code added by Michael Weeks. */ #include enum {CLUBS, DIAMONDS, HEARTS, SPADES} s; void print_suit(int s1) { switch (s1) { case CLUBS: printf("Clubs"); break; case DIAMONDS: printf("Diamonds"); break; case HEARTS: printf("Hearts"); break; case SPADES: printf("Spades"); break; default: printf("Unknown"); } } int main() { int i; i = DIAMONDS; /* i is now 1 */ printf("i = %d, which is ", i); print_suit(i); printf("\n"); s = 0; /* s is now 0 (CLUBS) */ printf("s = %d, which is ", s); print_suit(s); printf("\n"); s++; /* s is now 1 (DIAMONDS) */ printf("s = %d, which is ", s); print_suit(s); printf("\n"); i = s + 2; /* i is now 3 */ printf("i = %d, which is ", i); print_suit(i); printf("\n"); return 0; } mweeks@air:C_ch16$ gcc C_ch16_11.c mweeks@air:C_ch16$ ./a.out i = 1, which is Diamonds s = 0, which is Clubs s = 1, which is Diamonds i = 3, which is Spades mweeks@air:C_ch16$ cd ../C_Ch17 mweeks@air:C_Ch17$ cat C_ch17_1.c p = malloc(10000); if (p == NULL) { /* allocation failed; take appropriate action */ } if ((p = malloc(10000)) == NULL) { /* allocation failed; take appropriate action */ } mweeks@air:C_Ch17$ echo malloc = memory allocate malloc = memory allocate mweeks@air:C_Ch17$ cat C_ch17_2.c void *malloc(size_t size); "size_t" is an unsigned integer type defined in the library. mweeks@air:C_Ch17$ cat C_ch17_3.c "malloc" to allocate memory for a string of "n" characters: p = malloc(n + 1); cast "malloc" return value (not required): p = (char *) malloc(n + 1); mweeks@air:C_Ch17$ cat C_ch17_4.c #include #include struct rec { char LastName[41]; char FirstName[41]; }; struct rec *r; r = (struct rec *) malloc(sizeof(struct rec)); mweeks@air:C_Ch17$ cat malloc_ex1.c #include #include int main() { char *p; p = malloc(10000); if (p == NULL) { printf("Could not allocate memory\n"); exit(0); } printf("Memory allocation worked\n"); free(p); return 0; } mweeks@air:C_Ch17$ gcc malloc_ex1.c mweeks@air:C_Ch17$ ./a.out Memory allocation worked mweeks@air:C_Ch17$ gcc malloc_ex2.c malloc_ex2.c:9:5: error: array type 'char [10000]' is not assignable p = malloc(10000); ~ ^ malloc_ex2.c:12:12: warning: comparison between pointer and integer ('int' and 'void *') if (p[0] == NULL) { ~~~~ ^ ~~~~ 1 warning and 1 error generated. mweeks@air:C_Ch17$ cat malloc_ex2.c #include #include int main() { //char p[] = malloc(10000); char p[10000]; p = malloc(10000); // this does not really work if (p[0] == NULL) { printf("Could not allocate memory\n"); exit(0); } printf("Memory allocation worked\n"); free(p); return 0; } mweeks@air:C_Ch17$ cat malloc_ex2b.c #include #include int main() { //char p[] = malloc(10000); char *p; p = malloc(10000); if (p == NULL) { printf("Could not allocate memory\n"); exit(0); } printf("Memory allocation worked\n"); printf("p[123] is %d\n", p[123]); free(p); return 0; } mweeks@air:C_Ch17$ gcc malloc_ex2b.c mweeks@air:C_Ch17$ ./a.out Memory allocation worked p[123] is 0 mweeks@air:C_Ch17$ cat cat C_ch17_5.c cat: cat: No such file or directory p = malloc( ... ); q = malloc( ... ); p = q; // nothing points to p's original memory! // called "garbage" // No garbage collector in C // use "free" instead mweeks@air:C_Ch17$ cat cat C_ch17_6.c cat: cat: No such file or directory char *p = malloc(4); ... free(p); ... strcpy (p, "abc"); // WRONG mweeks@air:C_Ch17$ cat C_ch17_6.c char *p = malloc(4); ... free(p); ... strcpy (p, "abc"); // WRONG mweeks@air:C_Ch17$ grep fope *.c C_ch17_7.c: fp = fopen("test.txt", "r"); mweeks@air:C_Ch17$ vi C_ch17_8.c mweeks@air:C_Ch17$ vi C_ch17_9.c mweeks@air:C_Ch17$ vi C_ch17_10.c [No write since last change] Press ENTER or type command to continue mweeks@air:C_Ch17$ cat C_ch17_10.c #include int main() { // char *p = malloc(1024); // if (p == NULL) { printf("Could not allocate memory/n"); // exit(2); // } // fp = fopen("test.txt", "r"); // free(p); return 0; } mweeks@air:C_Ch17$ gcc C_ch17_10.c mweeks@air:C_Ch17$ ./a.out Could not allocate memory/nmweeks@air:C_Ch17$ mweeks@air:C_Ch17$ mweeks@air:C_Ch17$ vi C_ch17_10.c mweeks@air:C_Ch17$ gcc C_ch17_10.c mweeks@air:C_Ch17$ ./a.out Could not allocate memory mweeks@air:C_Ch17$ vi C_ch17_10.c mweeks@air:C_Ch17$ cat C_ch17_10.c #include int main() { char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); exit(1); } // .. do some file stuff // Close the stream fclose(fp); free(p); return 0; } mweeks@air:C_Ch17$ cp C_ch17_10.c C_ch17_10_v2.c mweeks@air:C_Ch17$ vi C_ch17_10_v2.c mweeks@air:C_Ch17$ cat C_ch17_10_v2.c #include int main() { char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); free(p); // Free up the memory before exiting exit(1); } // .. do some file stuff // Close the stream fclose(fp); free(p); return 0; } mweeks@air:C_Ch17$ cat C_ch17_7.c #include int main() { FILE *fp; fp = fopen("test.txt", "r"); char buff[255]; // n=255 // read fgets(buff, 255 , (FILE*)fp); printf("%s\n", buff ); // write // fputs("test.txt", fp); fclose(fp); return 0; } mweeks@air:C_Ch17$ cat C_ch17_8.c #include int main( int argc, char *argv[] ) { printf("Program name %s\n", argv[0]); if ( argc == 2 ) { printf("The argument supplied is %s\n", argv[1]); } else if ( argc > 2 ) { printf("Too many arguments supplied.\n"); } return 0; } mweeks@air:C_Ch17$ gcc C_ch17_8.c mweeks@air:C_Ch17$ ./a.out Program name ./a.out mweeks@air:C_Ch17$ ./a.out onething Program name ./a.out The argument supplied is onething mweeks@air:C_Ch17$ ./a.out onething twothings Program name ./a.out Too many arguments supplied. mweeks@air:C_Ch17$ cat C_ch17_9.c int count[26]={0}; ... c=buff[i]; if (c>='a' && c<='z') count[c-'a']++; else if (c >='A' && c <='Z') count[c- 'A']++; ... mweeks@air:C_Ch17$ exit exit Script done on Wed Oct 30 13:45:12 2024