Script started on Mon Oct 21 12:59:51 2024 mweeks@air:C_ch13$ cd ../more_programs/ mweeks@air:more_programs$ cat commandline2.c /* Simple example of a C program -Michael Weeks */ #include "stdio.h" #include "stdlib.h" #include "string.h" #define MAXCHARS 100 #define MAXSTRING 10 int main (int argc, char *argv[]) { // An array of MAXSTRING strings // Each string can have up to MAXCHARS characters. char arg_copy[MAXSTRING][MAXCHARS]; int i; // Copy the arguments as strings to arg_copy // This has 2 major problems: // 1. What if a string is > MAXCHARS in length? // 2. What if there are more than 10 arguments? for (i=0; i int main () { int num = 10; int *ptr1; int **ptr2; // Take the address of var ptr1 = # // Take the address of ptr1 using address of operator & ptr2 = &ptr1; // Print the value printf("Value of num = %d\n", num ); printf("Value available at *ptr1 = %d\n", *ptr1 ); printf("Value available at **ptr2 = %d\n", **ptr2); } mweeks@air:more_programs$ gcc double_ptr.c mweeks@air:more_programs$ ./a.out Value of num = 10 Value available at *ptr1 = 10 Value available at **ptr2 = 10 mweeks@air:more_programs$ mweeks@air:more_programs$ mweeks@air:more_programs$ echo 117 117 mweeks@air:more_programs$ cd ../C_ch13 mweeks@air:C_ch13$ cat test_string1.c #include int main() { char *str = "Hello World"; // char str[] = "Hello World"; printf("About to change the H in Hello to h.\n"); str[0] = 'h'; printf("%s\n", str); return 0; }mweeks@air:C_ch13$ echo non-standard behavior non-standard behavior mweeks@air:C_ch13$ gcc test_string1.c mweeks@air:C_ch13$ ./a.out About to change the H in Hello to h. Bus error: 10 mweeks@air:C_ch13$ cat test_string2.c #include int main() { // char *str = "Hello World"; char str[] = "Hello World"; printf("About to change the H in Hello to h.\n"); str[0] = 'h'; printf("%s\n", str); return 0; }mweeks@air:C_ch13$ mweeks@air:C_ch13$ gcc test_string2.c mweeks@air:C_ch13$ ./a.ot bash: ./a.ot: No such file or directory mweeks@air:C_ch13$ ./a.out About to change the H in Hello to h. hello World mweeks@air:C_ch13$ cat ch13_3.c #include int main(){ char date1[8] = "June 14"; char date1[] = "June 14"; // Length could be omitted char date1[8] = {'J', 'u', 'n', 'e', ' ', '1', '4', '\0'}; char *str = "June 14"; char date2[9] = "June 14"; } mweeks@air:C_ch13$ mweeks@air:C_ch13$ cat string_example.c #include #include int main(){ char date1[] = {'J', 'u', 'n', 'e', ' ', '1', '4', '\0'}; char date2[] = "October 18"; // strcpy -> strncpy // strlen -> strnlen // strcat -> strncat // strcpy //strcpy(date1, date2); strncpy(date1, date2, 7); printf("%s \n", date1); printf("%s \n", date2); return 0; } mweeks@air:C_ch13$ gcc string_example.c mweeks@air:C_ch13$ ./a.out October October 18 mweeks@air:C_ch13$ cat string_example2.c #include #include int main(){ char date1[] = {'J', 'u', 'n', 'e', ' ', '1', '4', '\0'}; char date2[] = "October 18"; // strcpy -> strncpy // strlen -> strnlen // strcat -> strncat // strcpy //strcpy(date1, date2); int c = strncmp(date1, date2, 7); printf("%s \n", date1); printf("%s \n", date2); printf("cmp returns %d \n", c); c = strncmp(date1, date1, 7); printf("cmp returns %d \n", c); return 0; } mweeks@air:C_ch13$ gcc string_example2.c mweeks@air:C_ch13$ ./a.out June 14 October 18 cmp returns -5 cmp returns 0 mweeks@air:C_ch13$ echo remember to check strncmp's result to 0 > ' remember to check strncmps result to 0 mweeks@air:C_ch13$ diff string_example1.c string_example2.c diff: string_example1.c: No such file or directory mweeks@air:C_ch13$ diff string_example.c string_example2.c 14c14 < strncpy(date1, date2, 7); --- > int c = strncmp(date1, date2, 7); 17a18,21 > printf("cmp returns %d \n", c); > > c = strncmp(date1, date1, 7); > printf("cmp returns %d \n", c); mweeks@air:C_ch13$ echo $? 1 mweeks@air:C_ch13$ echo "last command had status $?" last command had status 0 mweeks@air:C_ch13$ cd /usr/bin mweeks@air:bin$ du 694336 . mweeks@air:bin$ exit exit Script done on Mon Oct 21 13:45:35 2024