/* * pointers.c * * An example of strings and pointers that we did in class Nov 10 2020. * * $ make pointers * cc pointers.c -o pointers * $ ./pointers * a is 'gi!dbye' * p is 'hi!lo' * p[1] is 'i' * q[2] is 'n' * * * Jim Mahoney | cs.bennington.college */ #include #include #include int main(){ char a[10]; // This is a declaration of a block of memoery. // a[0] 0x1234 'h' // a[1] 0x1235 'i' char* p = malloc(10); // This declares a pointer ... to some memory. char* q = "Bond. James Bond."; strcpy(p, "hello"); // put string "hello" into p strcpy(a, "goodbye"); // similar for a *(a+1) = 'i'; // one way to put 'i' at a[0] a[2] = '!'; // SAME! *(p+1) = 'i'; // put 'i' at p[1] p[2] = '!'; printf(" a is '%s' \n", a); // address of a[0] printf(" p is '%s' \n", p); // p is address of p[0] printf(" p[1] is '%c' \n", p[1]); printf(" q[2] is '%c' \n", q[2]); return 0; }