Script started on Mon Oct 14 12:29:20 2024 mweeks@air:C_ch11$ echo code for today: 2112 code for today: 2112 mweeks@air: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; } mweeks@air: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; } mweeks@air:C_ch11$ gcc ptr_assign1.c mweeks@air:C_ch11$ ./a.ut bash: ./a.ut: No such file or directory mweeks@air:C_ch11$ ./a.out p points to 5 q points to 5 mweeks@air: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; } mweeks@air:C_ch11$ gcc pointer2a.c mweeks@air:C_ch11$ ./a.out p = 3 p = 4 a = 4 mweeks@air:C_ch11$ cat pointer2a_v2.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; } mweeks@air:C_ch11$ gcc pointer2a_v2.c mweeks@air:C_ch11$ ./a.out p = 3 p = 0 a = 3 mweeks@air:C_ch11$ cat ptr_as_arg.c /* ptr_as_arg.c */ #include void swap (int *p, int *q){ int temp=0; temp = *p; *p = *q; *q = temp; } int main (void) { int x = 1, y = 2; swap(&x, &y); printf("x= %d , y= %d",x,y); printf("\n"); return 0; } mweeks@air:C_ch11$ gcc ptr_as_arg.c mweeks@air:C_ch11$ ./a.out x= 2 , y= 1 mweeks@air:C_ch11$ cat ptr_as_ret.c /* ptr_as_ret.c See ch11 of Dr.King's book */ #include int *max(int *a, int *b) { if(*a > *b) return a; else return b; } int main (void) { int *p, x, y; p = max(&x, &y); printf("%d\n", *p); return 0; } mweeks@air:C_ch11$ vi ptr_as_ret.c mweeks@air:C_ch11$ cat ptr_as_ret.c /* ptr_as_ret.c See ch11 of Dr.King's book */ #include int *max(int *a, int *b) { if(*a > *b) return a; else return b; } int main (void) { int *p, x=3, y=5; p = max(&x, &y); printf("%d\n", *p); return 0; } mweeks@air:C_ch11$ gcc ptr_as_ret.c mweeks@air:C_ch11$ ./a.out 5 mweeks@air:C_ch11$ exit exit Script done on Mon Oct 14 14:02:29 2024