CSCD-255
2015-02-23


Arrays and Pointers (Chs. 25)

Back on 10 Feb we actually borrow from here when we did parallel arrays, since there is an array of character pointers.

int main()
{
   char* title[] = { "Harry Potter and The Sorcerer's Stone",
                     "Harry Potter And The Chamber Of Secrets",
                     "Harry Potter and The Prisoner of Azkaban",
                     "Harry Potter And The Goblet Of Fire",
                     "Harry Potter And The Order Of The Phoenix",
                     "Harry Potter and The Half-Blood Prince",
                     "Harry Potter and The Deathly Hallows" };

   int   pages[] = { 320, 341, 448, 752, 896, 652, 784 };
   float price[] = { 6.29, 6.64, 7.85, 8.99, 8.93, 8.85, 9.81 };
// Note:  this will be price in cents per page
   float priceperpage[7];

Text's Figure 25.1 on pointers:

short int vals[5] = {10, 20, 30, 40, 50};
short int *pVal   = vals;

Since our authors have a figure with addresses advancing by two bytes, I've changed the type to short int. and added another pointer, pVal.  The data type of vals is a const pointer to short int, while pVal is a changeable pointer to short int.

The dereferencing operator is extremely strong, but is overriden by parentheses.  So it is possible to use this expression:  *(ptr + 1).  This is, in fact, what you're doing when you write ptr[1].

Since C knows the data type to which ptr is pointing, it knows how many bytes are used for each cell and advances the pointer by as many bytes as required by the expression.

If double trouble[4] puts the array at address 2686704, then (trouble+1) refers to 2686712.

#include <stdio.h>

int main()
{
   double trouble[4] = { 4, 3, 2, 1 };

   printf("trouble is %d, and (trouble+1) is %d\n", trouble, (trouble+1));
   printf("*trouble + 1 gives %.lf\n", *trouble + 1);
   printf("*(trouble + 1) gives %.lf\n", *(trouble + 1));

   return 0;
}
/*    Run of the program

trouble is 2686704, and (trouble+1) is 2686712
*trouble + 1 gives 5
*(trouble + 1) gives 3

*/

demoPointerArray.txt   demoPointerArray.c   demoRun.txt   demoPointerArray.exe

Back when we did "Expanding Input and Output Operations (Chs. 18 and 19)" (3 Feb) we saw a serious problem with gets() — buffer overflow.

The specimen program was intentionally broken, reducing the sizes of the arrays to generate buffer overflows.  The explicit memory addresses are shown at the beginning.
broken.txt   broken.c   broken.exe

There are functions that restrict the size of the data transferred.  Our authors only mention fgets() in which the second parameter is the maximum size allowed.  [fgets will actually read one fewer character to allow for the mandatory trailing '\0' character.]  The third parameter is the file being read which we will get to later.  For now you just need to know that the name of your keyboard for data entry is stdin.
fixed.txt   fixed.c   fixed.exe

One behavior that our authors fail to mention is this:  If the input string is shorter than the space provided it will also include the character that ends the line, the newline '\n'.  On the other hand, if it does read the specified maximum number of characters it will leave the rest of the line in the file buffer.

If you continue in your C programming, you will be exposed to the safe versions of strcpy() and strcat()strncpy() and strncat().