Script started on Mon Oct 12 17:28:57 2026 macbook:C_ch11> cat addr_and_indirect.c /* addr_and_indirect.c */ #include int main (void) { int i, j, *p = &i; i = 1; printf("i = %d\n", i); printf("*p = %d\n", *p); *p = 4; printf("i = %d\n", i); printf("*p = %d\n", *p); j = *&i; printf("j = %d\n", j); return 0; } macbook:C_ch11> gcc addr_and_indirect.c macbook:C_ch11> echo predictions 1 1 4 4 4 predictions 1 1 4 4 4 macbook:C_ch11> ./a.out i = 1 *p = 1 i = 4 *p = 4 j = 4 macbook:C_ch11> cat bad_avg_sum.c void avg_sum (float a[], int n, float *avg, float *sum) { int i; sum = 0.0; // *sum = 0.0 for (i=0;i echo "what is the difference between float b=3.0; float *b=3.0;" what is the difference between float b=3.0; float *b=3.0; macbook:C_ch11> echo "float b1 = 3.0;" float b1 = 3.0; macbook:C_ch11> echo "float *b2;" float *b2; macbook:C_ch11> echo "*b2 = b1;" *b2 = b1; macbook:C_ch11> cd ~/Desktop macbook:Desktop> cat avg_sum_v2.c #include void avg_sum (float a[], int n, float *avg, float *sum) { int i; // This is the corrected version *sum = 0.0; // *sum = 0.0 for (i=0;i gcc avg_sum_v2.c macbook:Desktop> ./a.out 6291936479301977399843880960.000000 is avg, 75503235390440487363303964672.000000 is sum macbook:Desktop> cat avg_sum_v3.c #include void avg_sum (float a[], int n, float *avg, float *sum) { int i; // Corrected version // plus printfs to check values *sum = 0.0; // *sum = 0.0 for (i=0;i gcc avg_sum_v3.c macbook:Desktop> ./a.out a size is 12 0.000000 += 3.000000 3.000000 += 4.500000 7.500000 += 6.700000 14.200000 += 0.000000 14.200000 += 7858160213300908581191680.000000 7858160213300908581191680.000000 += -4346457425528750080.000000 7858155601614890153803776.000000 += 63897547522140047286124675072.000000 63905405539967542375760265216.000000 += 0.000000 63905405539967542375760265216.000000 += -0.000000 63905405539967542375760265216.000000 += 0.000000 63905405539967542375760265216.000000 += 0.000000 63905405539967542375760265216.000000 += 0.000000 avg = 63905405539967542375760265216.000000 / 12 = 5325450461663961864646688768.000000 5325450461663961864646688768.000000 is avg, 63905405539967542375760265216.000000 is sum macbook:Desktop> cat avg_sum_v4.c #include void avg_sum (float a[], int n, float *avg, float *sum) { int i; // Corrected version // plus printfs to check values *sum = 0.0; // *sum = 0.0 for (i=0;i gcc avg_sum_v4.c macbook:Desktop> ./a.out a size is 12 a size should be 3 0.000000 += 3.000000 3.000000 += 4.500000 7.500000 += 6.700000 14.200000 += 0.000000 14.200000 += -8311412856093016760254464.000000 -8311412856093016760254464.000000 += -0.000000 -8311412856093016760254464.000000 += 2352222551033577455055011840.000000 2343911186023726879478906880.000000 += 0.000000 2343911186023726879478906880.000000 += -0.000000 2343911186023726879478906880.000000 += 0.000000 2343911186023726879478906880.000000 += 0.000000 2343911186023726879478906880.000000 += 0.000000 avg = 2343911186023726879478906880.000000 / 12 = 195325932168643906623242240.000000 195325932168643906623242240.000000 is avg, 2343911186023726879478906880.000000 is sum macbook:Desktop> cd csc3320_Fall2026/programs/C_ch11 macbook:C_ch11> cat bad_ptr.c Don't return a ptr to local var int *f(void) { int a; ... return &a; } Once 'f' returns, the variable 'a' does not exist, so the pointer to it will be invalid. macbook:C_ch11> cat const_ex1.c void f(const int *p) { *p = 0; /* wrong: p is a pointer to a ``constant integer'' */ } macbook:C_ch11> cat dont_do_this.c #include int main() { int *p; printf("%d\n", *p); return 0; } macbook:C_ch11> gcc dont_do_this.c macbook:C_ch11> ./a.out -134919344 macbook:C_ch11> cat indirection.c /* indirection -- don't do this without assigning value first */ #include int main (void) { int *p; printf("%d", *p); printf("\n"); return 0; } macbook:C_ch11> cat maxmin.c /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* maxmin.c (Chapter 11, page 250) */ /* Finds the largest and smallest elements in an array */ #include #define N 10 void max_min(int a[], int n, int *max, int *min); int main(void) { int b[N], i, big, small; printf("Enter %d numbers: ", N); for (i = 0; i < N; i++) scanf("%d", &b[i]); max_min(b, N, &big, &small); printf("Largest: %d\n", big); printf("Smallest: %d\n", small); return 0; } void max_min(int a[], int n, int *max, int *min) { int i; *max = *min = a[0]; for (i = 1; i < n; i++) { if (a[i] > *max) *max = a[i]; else if (a[i] < *min) *min = a[i]; } } macbook:C_ch11> gcc maxmin.c macbook:C_ch11> ./a.out Enter 10 numbers: 11 12 13 14 15 16 17 18 19 100000 Largest: 100000 Smallest: 11 macbook:C_ch11> ./a.out Enter 10 numbers: 100000 12 13 14 15 16 17 18 11 19 Largest: 100000 Smallest: 11 macbook:C_ch11> cat types_of_pointers.c int *p; /* points only to integers */ char *str; /* points only to characters */ double *q; /* points only to doubles */ int i, j, a[10], *p, *q ; /* p and q point to integers */ int i=2, j, *p; /* p points nowhere */ p = &i; /* p points to integer variable i */ j = *p; /* same as j=i */ int i=2, *p=&i; /* p points to variable i */ int i, j, *p = &i; i = 1; printf("i = %d\n", i); printf("*p = %d\n", *p); *p = 4; printf("i = %d\n", i); printf("*p = %d\n", *p); j = *&i; printf("j = %d\n", j); macbook:C_ch11> cat ptr_assign1.c /* ptr_assign1.c */ #include int main (void) { int i=1, j=2, *p, *q; p = &i; q = p; *p = 5; // (Same as i = 5) printf("p points to %d\n", *p); printf("q points to %d\n", *q); return 0; } macbook:C_ch11> gcc ptr_assign1.c macbook:C_ch11> ./a.out p points to 5 q points to 5 macbook:C_ch11> cat pointer2a.c /* indirection -- don't do this without assigning value first */ #include int main (void) { int a = 3; int *p; p = &a; printf("p = %d\n", *p); (*p)++; printf("p = %d\n", *p); printf("a = %d\n", a); printf("\n"); return 0; } macbook:C_ch11> gcc pointer2a.c macbook:C_ch11> ./a.out p = 3 p = 4 a = 4 macbook:C_ch11> diff pointer2a.c pointer2a_v2.c 13c13 < (*p)++; --- > (p++); macbook:C_ch11> gcc pointer2a_v2.c macbook:C_ch11> ./a.out p = 3 p = 0 a = 3 macbook:C_ch11> cat q12.c cat: q12.c: No such file or directory macbook:C_ch11> cat ~/Desktop/q12.c #include int main() { // Each number is the sum of the previous two int Fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34}; int *p; p = &Fibonacci[6]; p = p + 2; printf("%d\n", *p); // What value will it print? return 0; } macbook:C_ch11> gcc ~/Desktop/q12.c macbook:C_ch11> ./a.out 21 macbook:C_ch11> cp ~/Desktop/q12.c ~/Desktop/q12a.c macbook:C_ch11> vi ~/Desktop/q12.c macbook:C_ch11> vi ~/Desktop/q12a.c macbook:C_ch11> gcc ~/Desktop/q12a.c macbook:C_ch11> ./a.out 21 macbook:C_ch11> vi ~/Desktop/q12a.c macbook:C_ch11> gcc ~/Desktop/q12a.c macbook:C_ch11> ./a.out 9 macbook:C_ch11> cat ~/Desktop/q12a.c #include int main() { // Each number is the sum of the previous two int Fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34}; int *p; p = &Fibonacci[6]; //p = p + 2; //(p++); //(p)++; (*p)++; printf("%d\n", *p); // What value will it print? return 0; } macbook:C_ch11> cd ../C_ch12 macbook:C_ch12> cat bad_idea.c #include #define NUM_ROWS 5 #define NUM_COLS 5 int main() { int a[NUM_ROWS][NUM_COLS]; int row, col; // row should not be = to NUM_ROWS for (row = 0; row < NUM_ROWS; row++) for (col = 0; col <= NUM_COLS; col++) { if (col < NUM_COLS) a[row][col] = 0; } // print for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) printf(" %d", a[row][col]); printf("\n"); } return 0; } macbook:C_ch12> gcc bad_idea.c macbook:C_ch12> ./a.out 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 macbook:C_ch12> vi bad_idea.c macbook:C_ch12> cat bad_idea.c #include #define NUM_ROWS 5 #define NUM_COLS 5 int main() { int a[NUM_ROWS][NUM_COLS]; int row, col; // row should not be = to NUM_ROWS for (row = 0; row < NUM_ROWS; row++) for (col = 0; col <= NUM_COLS; col++) { //if (col < NUM_COLS) a[row][col] = 0; } // print for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) printf(" %d", a[row][col]); printf("\n"); } return 0; } macbook:C_ch12> gcc bad_idea.c macbook:C_ch12> ./a.out 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 zsh: abort ./a.out macbook:C_ch12> vi bad_idea.c macbook:C_ch12> gcc bad_idea.c macbook:C_ch12> ./a.out done zsh: abort ./a.out macbook:C_ch12> cst ch12_1.c zsh: command not found: cst macbook:C_ch12> cat ch12_1.c #include int main(){ int a[8], *p, *q; p = &a[0]; // initialize the array a for (int i=0; i<8; i++) a[i] = i; //Add an integer to a pointer p = &a[2]; // When index is out of bound, q = p + 3; // accessing to *p may generate printf("%d\n", *q); //p += 4; // unexpected result q += 4; // unexpected result //printf("%d\n", *p); printf("%d\n", *q); //Subtract an integer from a pointer p = &a[6]; q = p - 3; printf("%d\n", *q); p -= 6; printf("%d\n", *p); return 0; } macbook:C_ch12> echo a = 0 1 2 3 4 5 6 7 a = 0 1 2 3 4 5 6 7 macbook:C_ch12> gcc ch12_1.c macbook:C_ch12> ./a.out 5 -2086831260 3 0 macbook:C_ch12> exit Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. Script done on Mon Oct 12 18:44:55 2026