printfarr p arr0 pn arr arr0 Now were declaring an int pointer q and setting it

Printfarr p arr0 pn arr arr0 now were declaring an

This preview shows page 1 - 3 out of 4 pages.

printf("arr = %p, &arr[0] = %p\n", arr, &arr[0]); // Now we're declaring an int pointer q and setting it to
Image of page 1
// point to the first element of the array (which arr already // points to). int *q = arr; // Here is a loop that shows that as we increment q (using q++) to point // to subsequent elements of arr, q is being incremented by 4 each time, // since q is an int pointer and int's are 4 bytes. That is, if q // points to an int, then in order for q to point to the next int, the // address stored in q to be incremented by 4. IMPORTANT: The compiler // takes care of adding 4 (rather than 1) to q when q++ is executed. The // programmer doesn't need to worry about it. for(int i=0; i< SIZE; i++) { printf("q = %p\n", q); // printing q in hex each time to show the increment by 4 *q = i*2; // writing to where q points to, which is an element of arr. q++; // incrementing q to point to the next element of arr. } // Using ordinary array notation, arr[i], we print out the // elements of arr. for(int i=0; i< SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); } void fun_with_strings() { printf("\nIn fun_with_strings()\n"); // There is no separate "string" type in C. A string is simply an // array of char's (bytes). In order to treat such an array as // a string, you need to write a 0 (not the character '0') at // the end of the string. This way, the printf function, and lots // of other functions that you will use to operate on strings, // knows where the end of the string is (at the 0). // declaring a variable as an array of char's, which we will // treat below as a string by making sure there's a 0 at the // end. char s[80]; // declaring a char pointer r and initializing it to point to // the start of the array s.
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 4 pages?

  • Fall '12
  • Barrett
  • ASCII, #include, #define, 4 bytes, one-byte

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture