/* Faan Tone Liu * Jan 22, 2003 * Demo program for comp 1672 * Demonstrates the relationship between pointers and arrays */ #include #include using namespace std; void output_list(float *, int); int main() { char myarray[] = "Old MacDonald had a farm"; char *cptr; int i; // set cptr to point to the beginning of the array myarray cptr = myarray; // traverse the array using cptr, ouput one character at a time for (i=0; i < strlen(myarray); i++) { cout << *(cptr+i); //cout << *cptr + i; This line is wrong, the order of operations // make it add i to character, instead of to the pointer } cout << endl; // Here's a loop that does the exact same thing as the above loop // The loop is controlled by the pointer now, instead of an integer variable for (cptr = myarray; *cptr != '\0'; cptr++) cout << *cptr; cout << endl; float arr[5] = {1, 2, 3, 4, 5}; float *fp; fp = arr; output_list(fp, 5); } void output_list(float *list, int len) { int i; for (i=0; i < 5; i++, list++) cout << *list; }