Script started on Wed Oct 16 12:29:15 2024 mweeks@air:C_ch11$ echo 9841 9841 mweeks@air: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); mweeks@air:C_ch11$ cat const_ex1.c void f(const int *p) { *p = 0; /* wrong: p is a pointer to a ``constant integer'' */ } mweeks@air: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. mweeks@air:C_ch11$ ls bad*c bad_avg_sum.c bad_ptr.c mweeks@air: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 int main(){ int a[10]; *a = 7; // modify a[0] *(a + 2) = 13; // modify a[2] while ( *a != 0 ){ //a++; // wrong a = a + 1; // this is not correct either } } mweeks@air:C_ch12$ echo "you can combine * with ++" you can combine * with ++ mweeks@air:C_ch12$ echo "*(p++)" *(p++) mweeks@air:C_ch12$ echo "compare pointers (p <= q) " compare pointers (p <= q) mweeks@air:C_ch12$ ls ch12_1.c ch12_4.c char_ptr_to_int.c ch12_2.c ch12_5.c find_largest.c ch12_3.c ch12_5b.c ch12_3_messed_up.c ch12_6.c mweeks@air:C_ch12$ cat char_ptr_to_int.c #include #define M 10 int main() { // Question posed by Avyuktkrishna, Fall 2023 int i, a[M] = {1, 2, 3, 4, 5, 6}; char *A; A = &a[0]; // These are incompatible types int sum = 0; for (i=0; i #define NUM_ROWS 5 #define NUM_COLS 5 int main(){ // Example: Initialize a 2D array // *Traditional method* int a[NUM_ROWS][NUM_COLS]; int row, col; for (row = 0; row < NUM_ROWS; row++) for (col = 0; col < NUM_COLS; col++) a[row][col] = 0; // *Using a pointer* int *p; for (p = &a[0][0]; p < &a[NUM_ROWS-1][NUM_COLS]; p++) *p = 0; return 0; } mweeks@air:C_ch12$ cd ~/Desktop/Projects/fox_and_geese/ mweeks@air:fox_and_geese$ make gcc -c NN.c cc -c -o fox_and_geese.o fox_and_geese.c gcc fox_and_geese.c -o fox_and_geese NN.o fileIO.o mystrings.o RNG.o mweeks@air:fox_and_geese$ exit exit Script done on Wed Oct 16 13:45:26 2024