home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / PTR_HELP.TXT < prev    next >
Text File  |  1994-04-03  |  43KB  |  1,120 lines

  1. .I 0 1117
  2.            UNDERSTANDING POINTERS (for beginners)
  3.                       by  Ted Jensen
  4.                        Version 0.0
  5.       This material is hereby placed in the public domain.
  6.                      September 5, 1993
  7.  
  8.                      TABLE OF CONTENTS
  9.  
  10.     INTRODUCTION;
  11.  
  12.     CHAPTER 1: What is a pointer?
  13.  
  14.     CHAPTER 2: Pointer types and Arrays
  15.  
  16.     CHAPTER 3: Pointers and Strings
  17.  
  18.     CHAPTER 4: More on Strings
  19.  
  20.     CHAPTER 5: Pointers and Structures
  21.  
  22.     CHAPTER 6: Some more on Strings, and Arrays of Strings
  23.  
  24.     EPILOG:
  25.  
  26. ==================================================================
  27.  
  28. INTRODUCTION:
  29.  
  30.     Over a period of several years of monitoring various
  31. telecommunication conferences on C I have noticed that one of the
  32. most difficult problems for beginners was the understanding of
  33. pointers.  After writing dozens of short messages in attempts to
  34. clear up various fuzzy aspects of dealing with pointers, I set up
  35. a series of messages arranged in "chapters" which I could draw
  36. from or email to various individuals who appeared to need help in
  37. this area.
  38.  
  39.     Recently, I posted all of this material in the FidoNet CECHO
  40. conference.  It received such a good acceptance, I decided to
  41. clean it up a little and submit it for inclusion in Bob Stout's
  42. SNIPPETS file.
  43.  
  44.     It is my hope that I can find the time to expand on this text
  45. in the future.  To that end, I am hoping that those who read this
  46. and find where it is lacking, or in error, or unclear, would
  47. notify me of same so the next version, should there be one, I can
  48. correct these deficiencys.
  49.  
  50.     It is impossible to acknowledge all those whose messages on
  51. pointers in various nets contributed to my knowledge in this
  52. area.  So, I will just say Thanks to All.
  53.  
  54.     I frequent the CECHO on FidoNet via RBBSNet and can be
  55. contacted via the echo itself or by email at:
  56.  
  57.      RBBSNet address 8:916/1.
  58.  
  59. I can also be reached via
  60.  
  61. Internet email at ted.jensen@spacebbs.com
  62.  
  63. Or     Ted Jensen
  64.        P.O. Box 324
  65.        Redwood City, CA 94064
  66.  
  67. ==================================================================
  68. CHAPTER 1: What is a pointer?
  69.  
  70.     One of the things beginners in C find most difficult to
  71. understand is the concept of pointers.  The purpose of this
  72. document is to provide an introduction to pointers and their use
  73. to these beginners.
  74.  
  75.     I have found that often the main reason beginners have a
  76. problem with pointers is that they have a weak or minimal feeling
  77. for variables, (as they are used in C).  Thus we start with a
  78. discussion of C variables in general.
  79.  
  80.     A variable in a program is something with a name, the value
  81. of which can vary.  The way the compiler and linker handles this
  82. is that it assigns a specific block of memory within the computer
  83. to hold the value of that variable.  The size of that block
  84. depends on the range over which the variable is allowed to vary.
  85. For example, on PC's the size of an integer variable is 2 bytes,
  86. and that of a long integer is 4 bytes.  In C the size of a
  87. variable type such as an integer need not be the same on all
  88. types of machines.
  89.  
  90.     When we declare a variable we inform the compiler of two
  91. things, the name of the variable and the type of the variable.
  92. For example, we declare a variable of type integer with the name
  93. k by writing:
  94.  
  95.     int k;
  96.  
  97.     On seeing the "int" part of this statement the compiler sets
  98. aside 2 bytes (on a PC) of memory to hold the value of the
  99. integer.  It also sets up a symbol table. And in that table it
  100. adds the symbol k and the address in memory where those 2 bytes
  101. were set aside.
  102.  
  103.     Thus, later if we write:
  104.  
  105.     k = 2;
  106.  
  107. at run time we expect that the value 2 will be placed in that
  108. memory location reserved for the storage of the value of k.
  109.  
  110.     In a sense there are two "values" associated with k, one
  111. being the value of the integer stored there (2 in the above
  112. example) and the other being the "value" of the memory location
  113. where it is stored, i.e. the address of k.  Some texts refer to
  114. these two values with the nomenclature rvalue (right value,
  115. pronounced "are value") and lvalue (left value, pronunced "el
  116. value") respectively.
  117.  
  118.     The lvalue is the value permitted on the left side of the
  119. assignment operator '=' (i.e. the address where the result of
  120. evaluation of the right side ends up).  The rvalue is that which
  121. is on the right side of the assignment statment, the '2' above.
  122. Note that rvalues cannot be used on the left side of the
  123. assignment statement.  Thus:    2 = k;   is illegal.
  124.  
  125.     Okay, now consider:
  126.  
  127.     int j, k;
  128.     k = 2;
  129.     j = 7;    <-- line 1
  130.     k = j;    <-- line 2
  131.  
  132.     In the above, the compiler interprets the j in line 1 as the
  133. address of the variable j (its lvalue) and creates code to copy
  134. the value 7 to that address.  In line 2, however, the j is
  135. interpreted as its rvalue (since it is on the right hand side of
  136. the assignment operator '=').  That is, here the j refers to the
  137. value _stored_ at the memory location set aside for j, in this
  138. case 7.  So, the 7 is copied to the address designated by the
  139. lvalue of k.
  140.  
  141.     In all of these examples, we are using 2 byte integers so all
  142. copying of rvalues from one storage location to the other is done
  143. by copying 2 bytes.  Had we been using long integers, we would be
  144. copying 4 bytes.
  145.  
  146.     Now, let's say that we have a reason for wanting a variable
  147. designed to hold an lvalue (an address).  The size required to
  148. hold such a value depends on the system.  On older desk top
  149. computers with 64K of memory total, the address of any point in
  150. memory can be contained in 2 bytes.  Computers with more memory
  151. would require more bytes to hold an address.  Some computers,
  152. such as the IBM PC might require special handling to hold a
  153. segment and offset under certain circumstances.  The actual size
  154. required is not too important so long as we have a way of
  155. informing the compiler that what we want to store is an address.
  156.  
  157.     Such a variable is called a "pointer variable" (for reasons
  158. which will hopefully become clearer a little later).  In C when
  159. we define a pointer variable we do so by preceding its name with
  160. an asterisk.  In C we also give our pointer a type which, in this
  161. case, refers to the type of data stored at the address we will be
  162. storing in our pointer.  For example, consider the variable
  163. definition:
  164.  
  165.     int *ptr;
  166.  
  167.     ptr is the _name_ of our variable (just as 'k' was the name
  168. of our integer variable).  The '*' informs the compiler that we
  169. want a pointer variable, i.e. to set aside however many bytes is
  170. required to store an address in memory.  The "int" says that we
  171. intend to use our pointer variable to store the address of an
  172. integer. Such a pointer is said to "point to" an integer.  Note,
  173. however, that when we wrote  "int k;" we did not give k a value.
  174. If this definiton was made outside of any function many compilers
  175. will initialize it to zero.  Simlarly, ptr has no value, that is
  176. we haven't stored an address in it in the above definition.  In
  177. this case, again if the definition is outside of any function, it
  178. is intialized to a value #defined by your compiler as NULL.  It
  179. is called a NULL pointer.  While in most cases NULL is #defined
  180. as zero, it need not be.  That is, different compilers handle
  181. this differently.  Also note that while zero is an integer, NULL
  182. need not be.
  183.  
  184.     But, back to using our new variable ptr.  Suppose now that we
  185. want to store in ptr the address of our integer variable k.  To
  186. do this we use the unary '&' operator and write:
  187.  
  188.     ptr = &k;
  189.  
  190.     What the '&' operator does is retrieve the lvalue (address)
  191. of k, even though k is on the right hand side of the assignment
  192. operator '=', and copies that to the contents of our pointer ptr.
  193. Now, ptr is said to "point to" k.  Bear with us now, there is
  194. only one more operator we need to discuss.
  195.  
  196.     The "dereferencing operator" is the asterisk and it is used
  197. as follows:
  198.  
  199.     *ptr = 7;
  200.  
  201. will copy 7 to the address pointed to by ptr.  Thus if ptr
  202. "points to" (contains the address of) k, the above statement will
  203. set the value of k to 7.  That is, when we use the '*' this way
  204. we are refering to the value of that which ptr is pointing
  205. at, not the value of the pointer itself.
  206.  
  207.     Similarly, we could write:
  208.  
  209.     printf("%d\n",*ptr);
  210.  
  211. to print to the screen the integer value stored at the address
  212. pointed to by "ptr".
  213.  
  214.     One way to see how all this stuff fits together would be to
  215. run the following program and then review the code and the output
  216. carefully.
  217.  
  218. -------------------------------------------------
  219. #include <stdio.h>
  220.  
  221. int j, k;
  222. int *ptr;
  223.  
  224.  
  225. int main(void)
  226. {
  227.    j = 1;
  228.    k = 2;
  229.    ptr = &k;
  230.    printf("\n");
  231.    printf("j has the value %d and is stored at %p\n",j,&j);
  232.    printf("k has the value %d and is stored at %p\n",k,&k);
  233.    printf("ptr has the value %p and is stored at %p\n",ptr,&ptr);
  234.    printf("The value of the integer pointed to by ptr is %d\n",
  235.            *ptr);
  236.    return 0;
  237. }
  238. ---------------------------------------
  239. To review:
  240.  
  241.     A variable is defined by giving it a type and a name (e.g.
  242.      int k;)
  243.  
  244.     A pointer variable is defined by giving it a type and a name
  245.      (e.g. int *ptr) where the asterisk tells the compiler that
  246.      the variable named ptr is a pointer variable and the type
  247.      tells the compiler what type the pointer is to point to
  248.      (integer in this case).
  249.  
  250.     Once a variable is defined, we can get its address by
  251.      preceding its name with the unary '&' operator, as in &k.
  252.  
  253.     We can "dereference" a pointer, i.e. refer to the value of
  254.      that which it points to, by using the unary '*' operator as
  255.      in *ptr.
  256.  
  257.     An "lvalue" of a variable is the value of its address, i.e.
  258.      where it is stored in memory.  The "rvalue" of a variable is
  259.      the value stored in that variable (at that address).
  260.  
  261. ==================================================================
  262. CHAPTER 2: Pointer types and Arrays
  263.  
  264.     Okay, let's move on.  Let us consider why we need to identify
  265. the "type" of variable that a pointer points to, as in:
  266.  
  267.         int *ptr;
  268.  
  269.     One reason for doing this is so that later, once ptr "points
  270. to" something, if we write:
  271.  
  272.         *ptr = 2;
  273.  
  274. the compiler will know how many bytes to copy into that memory
  275. location pointed to by ptr.  If ptr was defined as pointing to an
  276. integer, 2 bytes would be copied, if a long, 4 bytes would be
  277. copied.  Similarly for floats and doubles the appropriate number
  278. will be copied.  But, defining the type that the pointer points
  279. to permits a number of other interesting ways a compiler can
  280. interpret code.  For example, consider a block in memory
  281. consisting if ten integers in a row.  That is, 20 bytes of memory
  282. are set aside to hold 10 integer.
  283.  
  284.     Now, let's say we point our integer pointer ptr at the first
  285. of these integers.  Furthermore lets say that integer is located
  286. at memory location 100 (decimal).   What happens when we write:
  287.  
  288.     ptr + 1;
  289.  
  290.     Because the compiler "knows" this is a pointer (i.e. its
  291. value is an address) and that it points to an integer (its
  292. current address, 100, is the address of an integer), it adds 2 to
  293. ptr instead of 1, so the pointer "points to" the _next_
  294. _integer_, at memory location 102.  Similarly, were the ptr
  295. defined as a pointer to a long, it would add 4 to it instead of
  296. 1.  The same goes for other data types such as floats, doubles,
  297. or even user defined data types such as structures.
  298.  
  299.     Similarly, since ++ptr and ptr++ are both equivalent to
  300. ptr + 1 (though the point in the program when ptr is incremented
  301. may be different), incrementing a pointer using the unary ++
  302. operator, either pre- or post-, increments the address it stores
  303. by the amount sizeof(type) (i.e. 2 for an integer, 4 for a long,
  304. etc.).
  305.  
  306.     Since a block of 10 integers located contiguously in memory
  307. is, by definition, an array of integers, this brings up an
  308. interesting relationship between arrays and pointers.
  309.  
  310.     Consider the following:
  311.  
  312.     int my_array[] = {1,23,17,4,-5,100};
  313.  
  314.     Here we have an array containing 6 integers.  We refer to
  315. each of these integers by means of a subscript to my_array, i.e.
  316. using my_array[0] through my_array[5].  But, we could
  317. alternatively access them via a pointer as follows:
  318.  
  319.     int *ptr;
  320.  
  321.     ptr = &my_array[0];       /* point our pointer at the first
  322.                                  integer in our array */
  323.  
  324.     And then we could print out our array either using the array
  325. notation or by dereferencing our pointer.  The following code
  326. illustrates this:
  327. ------------------------------------------------------
  328. #include <stdio.h>
  329.  
  330. int my_array[] = {1,23,17,4,-5,100};
  331. int *ptr;
  332.  
  333. int main(void)
  334. {
  335.     int i;
  336.     ptr = &my_array[0];     /* point our pointer to the array */
  337.     printf("\n\n");
  338.     for(i = 0; i < 6; i++)
  339.     {
  340.       printf("my_array[%d] = %d   ",i,my_array[i]);   /*<-- A */
  341.       printf("ptr + %d = %d\n",i, *(ptr + i));        /*<-- B */
  342.     }
  343.     return 0;
  344. }
  345. ----------------------------------------------------
  346.    Compile and run the above program and carefully note lines A
  347. and B and that the program prints out the same values in either
  348. case.  Also note how we dereferenced our pointer in line B, i.e.
  349. we first added i to it and then dereferenced the the new pointer.
  350. Change line B to read:
  351.  
  352.      printf("ptr + %d = %d\n",i, *ptr++);
  353.  
  354. and run it again... then change it to:
  355.  
  356.      printf("ptr + %d = %d\n",i, *(++ptr));
  357.  
  358. and try once more.  Each time try and predict the outcome and
  359. carefully look at the actual outcome.
  360.  
  361.     In C, the standard states that wherever we might use
  362. &var_name[0] we can replace that with var_name, thus in our code
  363. where we wrote:
  364.  
  365.         ptr = &my_array[0];
  366.  
  367.     we can write:
  368.  
  369.         ptr = my_array;     to achieve the same result.
  370.  
  371.     This leads many texts to state that the name of an array is a
  372. pointer.  While this is true, I prefer to mentally think "the
  373. name of the array is a _constant_ pointer".  Many beginners
  374. (including myself when I was learning) forget that _constant_
  375. qualifier.  In my opinon this leads to some confusion.  For
  376. example, while we can write ptr = my_array; we cannot write
  377.  
  378.     my_array = ptr;
  379.  
  380.     The reason is that the while ptr is a variable, my_array is a
  381. constant.  That is, the location at which the first element of
  382. my_array will be stored cannot be changed once my_array[] has
  383. been declared.
  384.  
  385. Modify the example program above by changing
  386.  
  387.     ptr = &my_array[0];     to     ptr = my_array;
  388.  
  389. and run it again to verify the results are identical.
  390.  
  391.     Now, let's delve a little further into the difference between
  392. the names "ptr" and "my_array" as used above.  We said that
  393. my_array is a constant pointer.  What do we mean by that?  Well,
  394. to understand the term "constant" in this sense, let's go back to
  395. our definition of the term "variable".  When we define a variable
  396. we set aside a spot in memory to hold the value of the
  397. appropriate type.  Once that is done the name of the variable can
  398. be interpreted in one of two ways.  When used on the left side of
  399. the assignment operator, the compiler interprets it as the memory
  400. location to which to move that which lies on the right side of
  401. the assignment operator.  But, when used on the right side of the
  402. assignment operator, the name of a variable is interpreted to
  403. mean the contents stored at that memory address set aside to hold
  404. the value of that variable.
  405.  
  406.     With that in mind, let's now consider the simplest of
  407. constants, as in:
  408.  
  409.     int i, k;
  410.     i = 2;
  411.  
  412.     Here, while "i" is a variable and then occupies space in the
  413. data portion of memory, "2" is a constant and, as such, instead
  414. of setting aside memory in the data segment, it is imbedded
  415. directly in the code segment of memory.  That is, while writing
  416. something like k = i;  tells the compiler to create code which at
  417. run time will look at memory location &i to determine the value
  418. to be moved to k, code created by  i = 2;  simply puts the '2' in
  419. the code and there is no referencing of the data segment.
  420.  
  421.     Similarly, in the above, since "my_array" is a constant, once
  422. the compiler establishes where the array itself is to be stored,
  423. it "knows" the address of my_array[0] and on seeing:
  424.  
  425.     ptr = my_array;
  426.  
  427. it simply uses this address as a constant in the code segment and
  428. there is no referencing of the data segment beyond that.
  429.  
  430.     Well, that's a lot of technical stuff to digest and I don't
  431. expect a beginner to understand all of it on first reading.  With
  432. time and experimentation you will want to come back and re-read
  433. the first 2 chapters.  But for now, let's move on to the
  434. relationship between pointers, character arrays, and strings.
  435.  
  436. ==================================================================
  437. CHAPTER 3:  Pointers and Strings
  438.  
  439.     The study of strings is useful to further tie in the
  440. relationship between pointers and arrays.  It also makes it easy
  441. to illustrate how some of the standard C string functions can be
  442. implemented. Finally it illustrates how and when pointers can and
  443. should be passed to functions.
  444.  
  445.     In C, strings are arrays of characters.  This is not
  446. necessarily true in other languages.  In Pascal or (most versions
  447. of) Basic, strings are treated differently from arrays.  To start
  448. off our discussion we will write some code which, while preferred
  449. for illustrative purposes, you would probably never write in an
  450. actual program.  Consider, for example:
  451.  
  452.     char my_string[40];
  453.  
  454.     my_string[0] = 'T';
  455.     my_string[1] = 'e';
  456.     my_string[2] = 'd':
  457.     my_string[3] = '\0';
  458.  
  459.     While one would never build a string like this, the end
  460. result is a string in that it is an array of characters
  461. _terminated_with_a_nul_character_.  By definition, in C, a string
  462. is an array of characters terminated with the nul character. Note
  463. that "nul" is _not_ the same as "NULL".  The nul refers to a zero
  464. as is defined by the escape sequence '\0'.  That is it occupies
  465. one byte of memory.  The NULL, on the other hand, is the value of
  466. an uninitialized pointer and pointers require more than one byte
  467. of storage.  NULL is #defined in a header file in your C
  468. compiler, nul may not be #defined at all.
  469.  
  470.     Since writing the above code would be very time consuming, C
  471. permits two alternate ways of achieving the same thing.  First,
  472. one might write:
  473.  
  474.     char my_string[40] = {'T', 'e', 'd', '\0',};
  475.  
  476.     But this also takes more typing than is convenient.  So, C
  477. permits:
  478.  
  479.     char my_string[40] = "Ted";
  480.  
  481.     When the double quotes are used, instead of the single quotes
  482. as was done in the previous examples, the nul character ( '\0' )
  483. is automatically appended to the end of the string.
  484.  
  485.     In all of the above cases, the same thing happens.  The
  486. compiler sets aside an contiguous block of memory 40 bytes long
  487. to hold characters and initialized it such that the first 4
  488. characters are Ted\0.
  489.  
  490.     Now, consider the following program:
  491.  
  492. ------------------program 3.1-------------------------------------
  493. #include <stdio.h>
  494.  
  495. char strA[80] = "A string to be used for demonstration purposes";
  496. char strB[80];
  497.  
  498. int main(void)
  499. {
  500.    char *pA;     /* a pointer to type character */
  501.    char *pB;     /* another pointer to type character */
  502.    puts(strA);   /* show string A */
  503.    pA = strA;    /* point pA at string A */
  504.    puts(pA);     /* show what pA is pointing to */
  505.    pB = strB;    /* point pB at string B */
  506.    putchar('\n');       /* move down one line on the screen */
  507.    while(*pA != '\0')   /* line A (see text) */
  508.    {
  509.      *pB++ = *pA++;     /* line B (see text) */
  510.    }
  511.    *pB = '\0';          /* line C (see text) */
  512.    puts(strB);          /* show strB on screen */
  513.    return 0;
  514. }
  515. --------- end program 3.1 -------------------------------------
  516.  
  517.     In the above we start out by defining two character arrays of
  518. 80 characters each.  Since these are globally defined, they are
  519. initialized to all '\0's first.  Then, strA has the first 42
  520. characters initialized to the string in quotes.
  521.  
  522.     Now, moving into the code, we define two character pointers
  523. and show the string on the screen.  We then "point" the ponter pA
  524. at strA.  That is, by means of the assignment statement we copy
  525. the address of strA[0] into our variable pA.  We now use puts()
  526. to show that which is pointed to by pA on the screen.  Consider
  527. here that the function prototype for puts() is:
  528.  
  529.     int puts(const char *s);
  530.  
  531.     For the moment, ignore the "const".  The parameter passed to
  532. puts is a pointer, that is the _value_ of a pointer (since all
  533. parameters in C are passed by value), and the value of a pointer
  534. is the address to which it points, or, simply, an address.  Thus
  535. when we write:
  536.  
  537.     puts(strA);        as we have seen, we are passing the
  538.  
  539. address of strA[0].  Similarly, when we write:
  540.  
  541.     puts(pA);          we are passing the same address, since
  542.  
  543. we have set pA = strA;
  544.  
  545.     Given that, follow the code down to the while() statement on
  546. line A.  Line A states:
  547.  
  548.     While the character pointed to by pA (i.e. *pA) is not a nul
  549. character (i.e. the terminating '\0'), do the following:
  550.  
  551.     line B states:  copy the character pointed to by pA to the
  552. space pointed to by pB, then increment pA so it points to the
  553. next character and pB so it points to the next space.
  554.  
  555.     Note that when we have copied the last character, pA now
  556. points to the terminating nul character and the loop ends.
  557. However, we have not copied the nul character.  And, by
  558. definition a string in C _must_ be nul terminated.  So, we add
  559. the nul character with line C.
  560.  
  561.     It is very educational to run this program with your debugger
  562. while watching strA, strB, pA and pB and single stepping through
  563. the program.  It is even more educational if instead of simply
  564. defining strB[] as has been done above, initialize it also with
  565. something like:
  566.  
  567.  strB[80] = "12345678901234567890123456789012345678901234567890"
  568.  
  569. where the number of digits used is greater than the length of
  570. strA and then repeat the single stepping procedure while watching
  571. the above variables.  Give these things a try!
  572.  
  573.     Of course, what the above program illustrates is a simple way
  574. of copying a string.  After playing with the above until you have
  575. a good understanding of what is happening, we can proceed to
  576. creating our own replacement for the standard strcpy() that comes
  577. with C.  It might look like:
  578.  
  579.    char *my_strcpy(char *destination, char *source)
  580.    {
  581.         char *p = destination
  582.         while (*source != '\0')
  583.         {
  584.            *p++ = *source++;
  585.         }
  586.         *p = '\0';
  587.         return destination.
  588.    }
  589.  
  590.     In this case, I have followed the practice used in the
  591. standard routine of returning a pointer to the destination.
  592.  
  593.     Again, the function is designed to accept the values of two
  594. character pointers, i.e. addresses, and thus in the previous
  595. program we could write:
  596.  
  597. int main(void)
  598. {
  599.   my_strcpy(strB, strA);
  600.   puts(strB);
  601. }
  602.  
  603.     I have deviated slightly from the form used in standard C
  604. which would have the prototype:
  605.  
  606.     char *my_strcpy(char *destination, const char *source);
  607.  
  608.     Here the "const" modifier is used to assure the user that the
  609. function will not modify the contents pointed to by the source
  610. pointer.  You can prove this by modifying the function above, and
  611. its prototype, to include the "const" modifier as shown.  Then,
  612. within the function you can add a statement which attempts to
  613. change the contents of that which is pointed to by source, such
  614. as:
  615.  
  616.     *source = 'X';
  617.  
  618. which would normally change the first character of the string to
  619. an X.  The const modifier should cause your compiler to catch
  620. this as an error.  Try it and see.
  621.  
  622.     Now, let's consider some of the things the above examples
  623. have shown us.  First off, consider the fact that *ptr++ is to be
  624. interpreted as returning the value pointed to by ptr and then
  625. incrementing the pointer value.  On the other hand, note that
  626. this has to do with the precedence of the operators.  Were we to
  627. write (*ptr)++ we would increment, not the pointer, but that
  628. which the pointer points to!  i.e. if used on the first character
  629. of the above example string the 'T' would be incremented to a
  630. 'U'.  You can write some simple example code to illustrate this.
  631.  
  632.     Recall again that a string is nothing more than an array
  633. of characters.  What we have done above is deal with copying
  634. an array.  It happens to be an array of characters but the
  635. technique could be applied to an array of integers, doubles,
  636. etc.  In those cases, however, we would not be dealing with
  637. strings and hence the end of the array would not be
  638. _automatically_ marked with a special value like the nul
  639. character.  We could implement a version that relied on a
  640. special value to identify the end. For example, we could
  641. copy an array of postive integers by marking the end with a
  642. negative integer.  On the other hand, it is more usual that
  643. when we write a function to copy an array of items other
  644. than strings we pass the function the number of items to be
  645. copied as well as the address of the array, e.g. something
  646. like the following prototype might indicate:
  647.  
  648.     void int_copy(int *ptrA, int *ptrB, int nbr);
  649.  
  650. where nbr is the number of integers to be copied.  You might want
  651. to play with this idea and create an array of integers and see if
  652. you can write the function int_copy() and make it work.
  653.  
  654.     Note that this permits using functions to manipulate very
  655. large arrays.  For example, if we have an array of 5000 integers
  656. that we want to manipulate with a function, we need only pass to
  657. that function the address of the array (and any auxiliary
  658. information such as nbr above, depending on what we are doing).
  659. The array itself does _not_ get passed, i.e. the whole array is
  660. not copied and put on the stack before calling the function, only
  661. its address is sent.
  662.  
  663.     Note that this is different from passing, say an integer, to
  664. a function.  When we pass an integer we make a copy of the
  665. integer, i.e. get its value and put it on the stack.  Within the
  666. function any manipulation of the value passed can in no way
  667. effect the original integer.  But, with arrays and pointers we
  668. can pass the address of the variable and hence manipulate the
  669. values of of the original variables.
  670.  
  671. ==================================================================
  672. CHAPTER 4: More on Strings
  673.  
  674.     Well, we have progressed quite aways in a short time!  Let's
  675. back up a little and look at what was done in Chapter 3 on
  676. copying of strings but in a different light.  Consider the
  677. following function:
  678.  
  679.    char *my_strcpy(char dest[], char source[])
  680.    {
  681.         int i = 0;
  682.  
  683.         while (source[i] != '\0')
  684.         {
  685.            dest[i] = source[i];
  686.            i++;
  687.         }
  688.         dest[i] = '\0';
  689.         return dest;
  690.    }
  691.  
  692.     Recall that strings are arrays of characters.  Here we have
  693. chosen to use array notation instead of pointer notation to do
  694. the actual copying.  The results are the same, i.e. the string
  695. gets copied using this notation just as accurately as it did
  696. before.  This raises some interesting points which we will
  697. discuss.
  698.  
  699.     Since parameters are passed by value, in both the passing of
  700. a character pointer or the name of the array as above, what
  701. actually gets passed is the address of the first element of each
  702. array.  Thus, the numerical value of the parameter passed is the
  703. same whether we use a character pointer or an array name as a
  704. parameter.  This would tend to imply that somehow:
  705.  
  706.         source[i]  is the same as  *(p+i);
  707.  
  708. In fact, this is true, i.e wherever one writes   a[i]  it can be
  709. replaced with  *(a + i) without any problems.  In fact, the
  710. compiler will create the same code in either case.   Now, looking
  711. at this last expression, part of it..  (a + i)  is a simple
  712. addition using the + operator and the rules of c state that such
  713. an expression is commutative.  That is   (a + i) is identical to
  714. (i + a).  Thus we could write *(i + a) just as easily as
  715. *(a + i).
  716.  
  717.     But *(i + a) could have come from i[a] !  From all of this
  718. comes the curious truth that if:
  719.  
  720.     char a[20];
  721.     int i;
  722.  
  723.     writing    a[3] = 'x';   is the same as writing
  724.  
  725.                3[a] = 'x';
  726.  
  727.     Try it!  Set up an array of characters, integers or longs,
  728. etc. and assigned the 3rd or 4th element a value using the
  729. conventional approach and then print out that value to be sure
  730. you have that working.  Then reverse the array notation as I have
  731. done above.  A good compiler will not balk and the results will
  732. be identical.   A curiosity... nothing more!
  733.  
  734.     Now, looking at our function above, when we write:
  735.  
  736.         dest[i] = source[i];
  737.  
  738.     this gets interpreted by C to read:
  739.  
  740.         *(dest + i) = *(source + i);
  741.  
  742.     But, this takes 2 additions for each value taken on by i.
  743. Additions, generally speaking, take more time than
  744. incrementations (such as those done using the ++ operator as in
  745. i++).  This may not be true in modern optimizing compilers, but
  746. one can never be sure.  Thus, the pointer version may be a bit
  747. faster than the array version.
  748.  
  749.     Another way to speed up the pointer version would be to
  750. change:
  751.  
  752.     while (*source != '\0')     to simply    while (*source)
  753.  
  754. since the value within the parenthesis will go to zero (FALSE) at
  755. the same time in either case.  
  756.  
  757.     At this point you might want to experiment a bit with writing
  758. some of your own programs using pointers.  Manipulating strings
  759. is a good place to experiment.  You might want to write your own
  760. versions of such standard functions as:
  761.  
  762.             strlen();
  763.             strcat();
  764.             strchr();
  765.  
  766. and any others you might have on your system.
  767.  
  768.     We will come back to strings and their manipulation through
  769. pointers in a future chapter.  For now, let's move on and discuss
  770. structures for a bit.
  771.  
  772. ==================================================================
  773. CHAPTER 5: Pointers and Structures
  774.  
  775.     As you may know, we can declare the form of a block of data
  776. containing different data types by means of a structure
  777. declaration.  For example, a personnel file might contain
  778. structures which look something like:
  779.  
  780.   struct tag{
  781.        char lname[20];        /* last name */
  782.        char fname[20];        /* first name */
  783.        int age;               /* age */
  784.        float rate;            /* e.g. 12.75 per hour */
  785.        };
  786.  
  787.     Let's say we have an bunch of these structures in a disk file
  788. and we want to read each one out and print out the first and last
  789. name of each one so that we can have a list of the people in our
  790. files.  The remaining information will not be printed out.  We
  791. will want to do this printing with a function call and pass to
  792. that function a pointer to the structure at hand.  For
  793. demonstration purposes I will use only one structure for now. But
  794. realize the goal is the writing of the function, not the reading
  795. of the file which, presumably, we know how to do.
  796.  
  797.     For review, recall that we can access structure members with
  798. the dot operator as in:
  799.  
  800. --------------- program 5.1 ------------------
  801. #include <stdio.h>
  802. #include <string.h>
  803.  
  804. struct tag{
  805.        char lname[20];      /* last name */
  806.        char fname[20];      /* first name */
  807.        int age;             /* age */
  808.        float rate;          /* e.g. 12.75 per hour */
  809.        };
  810.  
  811. struct tag my_struct;       /* declare the structure m_struct */
  812.  
  813. int main(void)
  814. {
  815.   strcpy(my_struct.lname,"Jensen");
  816.   strcpy(my_struct.fname,"Ted");
  817.   printf("\n%s ",my_struct.fname);
  818.   printf("%s\n",my_struct.lname);
  819.   return 0;
  820. }
  821. -------------- end of program 5.1 --------------
  822.  
  823.     Now, this particular structure is rather small compared to
  824. many used in C programs.  To the above we might want to add:
  825.  
  826.   date_of_hire;
  827.   date_of_last_raise;
  828.   last_percent_increase;
  829.   emergency_phone;
  830.   medical_plan;
  831.   Social_S_Nbr;
  832.   etc.....
  833.  
  834.     Now, if we have a large number of employees, what we want to
  835. do manipulate the data in these structures by means of functions.
  836. For example we might want a function print out the name of any
  837. structure passed to it.  However, in the original C (Kernighan &
  838. Ritchie) it was not possible to pass a structure, only a pointer
  839. to a structure could be passed.  In ANSI C, it is now permissible
  840. to pass the complete structure.  But, since our goal here is to
  841. learn more about pointers, we won't pursue that.
  842.  
  843.     Anyway, if we pass the whole structure it means there must be
  844. enough room on the stack to hold it.  With large structures this
  845. could prove to be a problem.  However, passing a pointer uses a
  846. minimum amount of stack space.
  847.  
  848.     In any case, since this is a discussion of pointers, we will
  849. discuss how we go about passing a pointer to a structure and then
  850. using it within the function.
  851.  
  852.     Consider the case described, i.e. we want a function that
  853. will accept as a parameter a pointer to a structure and from
  854. within that function we want to access members of the structure.
  855. For example we want to print out the name of the employee in our
  856. example structure.
  857.  
  858.     Okay, so we know that our pointer is going to point to a
  859. structure declared using struct tag.  We define such a pointer
  860. with the definition:
  861.  
  862.     struct tag *st_ptr;
  863.  
  864. and we point it to our example structure with:
  865.  
  866.     st_ptr = &my_struct;
  867.  
  868.     Now, we can access a given member by de-referencing the
  869. pointer. But, how do we de-reference the pointer to a structure?
  870. Well, consider the fact that we might want to use the pointer to
  871. set the age of the employee.  We would write:
  872.  
  873.     (*st_ptr).age = 63;
  874.  
  875.     Look at this carefully.  It says, replace that within the
  876. parenthesis with that which st_ptr points to, which is the
  877. structure my_struct.  Thus, this breaks down to the same as
  878. my_struct.age.
  879.  
  880.     However, this is a fairly often used expression and the
  881. designers of C have created an alternate syntax with the same
  882. meaning which is:
  883.  
  884.     st_ptr->age = 63;
  885.  
  886.     With that in mind, look at the following program:
  887.  
  888. ------------ program 5.2 --------------
  889.  
  890. #include <stdio.h>
  891. #include <string.h>
  892.  
  893. struct tag{                   /* the structure type */
  894.        char lname[20];        /* last name */
  895.        char fname[20];        /* first name */
  896.        int age;               /* age */
  897.        float rate;            /* e.g. 12.75 per hour */
  898.        };
  899.  
  900. struct tag my_struct;         /* define the structure */
  901.  
  902. void show_name(struct tag *p);    /* function prototype */
  903.  
  904. int main(void)
  905. {
  906.   struct tag *st_ptr;         /* a pointer to a structure */
  907.   st_ptr = &my_struct;        /* point the pointer to my_struct */
  908.   strcpy(my_struct.lname,"Jensen");
  909.   strcpy(my_struct.fname,"Ted");
  910.   printf("\n%s ",my_struct.fname);
  911.   printf("%s\n",my_struct.lname);
  912.   my_struct.age = 63;
  913.   show_name(st_ptr);          /* pass the pointer */
  914.   return 0;
  915. }
  916.  
  917.  
  918. void show_name(struct tag *p)
  919. {
  920.   printf("\n%s ", p->fname);     /* p points to a structure */
  921.   printf("%s ", p->lname);
  922.   printf("%d\n", p->age);
  923. }
  924. -------------------- end of program 5.2 ----------------
  925.  
  926.     Again, this is a lot of information to absorb at one time.
  927. The reader should compile and run the various code snippets and
  928. using a debugger monitor things like my_struct and p while single
  929. stepping through the main and following the code down into the
  930. function to see what is happening.
  931.  
  932. ==================================================================
  933. CHAPTER 6:  Some more on Strings, and Arrays of Strings
  934.  
  935.    Well, let's go back to strings for a bit.  In the following
  936. all assignments are to be understood as being global, i.e. made
  937. outside of any function, including main.
  938.  
  939.    We pointed out in an earlier chapter that we could write:
  940.  
  941.    char my_string[40] = "Ted";
  942.  
  943. which would allocate space for a 40 byte array and put the string
  944. in the first 4 bytes (three for the characters in the quotes and
  945. a 4th to handle the terminating '\0'.
  946.  
  947.     Actually, if all we wanted to do was store the name "Ted" we
  948. could write:
  949.  
  950.       char my_name[] = "Ted";
  951.  
  952. and the compiler would count the characters, leave room for the
  953. nul character and store the total of the four characters in memory
  954. the location of which would be returned by the array name, in this
  955. case my_string.
  956.  
  957.     In some code, instead of the above, you might see:
  958.  
  959.      char *my_name = "Ted";
  960.  
  961. which is an alternate approach.  Is there a difference between
  962. these?  The answer is.. yes.  Using the array notation 4 bytes of
  963. storage in the static memory block are taken up, one for each
  964. character and one for the nul character.  But, in the pointer
  965. notation the same 4 bytes required, _plus_ N bytes to store the
  966. pointer variable my_name (where N depends on the system but is
  967. usually a minimum of 2 bytes and can be 4 or more).
  968.  
  969.     In the array notation, my_name is a constant (not a
  970. variable).  In the pointer notation my_name is a variable.  As to
  971. which is the _better_ method, that depends on what you are going
  972. to do within the rest of the program.
  973.  
  974.     Let's now go one step further and consider what happens if
  975. each of these definitions are done within a function as opposed
  976. to globally outside the bounds of any function.
  977.  
  978. void my_function_A(char *ptr)
  979. {
  980.   char a[] = "ABCDE";
  981.   .
  982.   .
  983. }
  984.  
  985. void my_function_B(char *ptr)
  986. {
  987.   char *cp = "ABCDE";
  988.   .
  989.   .
  990. }
  991.  
  992.     Here we are dealing with automatic variables in both cases.
  993. In my_function_A the automatic variable is the character array
  994. a[]. In my_function_B it is the pointer cp.  While C is designed
  995. in such a way that a stack is not required on those processors
  996. which don't use them, my particular processor (80286) has a
  997. stack.  I wrote a simple program incorporating functions similar
  998. to those above and found that in my_function_A the 5 characters
  999. in the string were all stored on the stack.  On the other hand,
  1000. in my_function_B, the 5 characters were stored in the data space
  1001. and the pointer was stored on the stack.
  1002.  
  1003.     By making a[] static I could force the compiler to place the
  1004. 5 characters in the data space as opposed to the stack.  I did
  1005. this exercise to point out just one more difference between
  1006. dealing with arrays and dealing with pointers.  By the way, array
  1007. initialization of automatic variables as I have done in
  1008. my_function_A was illegal in the older K&R C and only "came of
  1009. age" in the newer ANSI C.  A fact that may be important when one
  1010. is considering portabilty and backwards compatability.
  1011.  
  1012.     As long as we are discussing the relationship/differences
  1013. between pointers and arrays, let's move on to multi-dimensional
  1014. arrays.  Consider, for example the array:
  1015.  
  1016.     char multi[5][10];
  1017.  
  1018.     Just what does this mean?   Well, let's consider it in the
  1019. following light.
  1020.  
  1021.         char multi[5][10];
  1022.         ^^^^^^^^^^^^^
  1023.  
  1024.     If we take the first, underlined, part above and consider it
  1025. to be a variable in its own right, we have an array of 10
  1026. characters with the "name"  multi[5].  But this name, in itself,
  1027. implies an array of 5 somethings.  In fact, it means an array of
  1028. five 10 character arrays.  Hence we have an array of arrays.  In
  1029. memory we might think of this as looking like:
  1030.  
  1031.       multi[0] = "0123456789"
  1032.       multi[1] = "abcdefghij"
  1033.       multi[2] = "ABCDEFGHIJ"
  1034.       multi[3] = "9876543210"
  1035.       multi[4] = "JIHGFEDCBA"
  1036.  
  1037. with individual elements being, for example:
  1038.  
  1039.       multi[0][3] = '3'
  1040.       multi[1][7] = 'h'
  1041.       multi[4][0] = 'J'
  1042.  
  1043.     Since arrays are to be contiguous, our actual memory block
  1044. for the above should look like:
  1045.  
  1046.     "0123456789abcdefghijABCDEFGHIJ9876543210JIHGFEDCBA"
  1047.  
  1048.     Now, the compiler knows how many columns are present in the
  1049. array so it can interpret multi + 1 as the address of the 'a' in
  1050. the 2nd row above.  That is, it adds 10, the number of columns,
  1051. to get this location.  If we were dealing with integers and an
  1052. array with the same dimension the compiler would add
  1053. 10*sizeof(int) which, on my machine, would be 20.  Thus, the
  1054. address of the "9" in the 4th row above would be &multi[3][0] or
  1055. *(multi + 3) in pointer notation.  To get to the content of the
  1056. 2nd element in row 3 we add 1 to this address and dereference the
  1057. result as in
  1058.  
  1059.     *(*(multi + 3) + 1)
  1060.  
  1061.     With a little thought we can see that:
  1062.  
  1063.     *(*(multi + row) + col)    and
  1064.     multi[row][col]            yield the same results.
  1065.  
  1066.     The following program illustrates this using integer arrays
  1067. instead of character arrays.
  1068.  
  1069. ------------------- program 6.1 ----------------------
  1070. #include <stdio.h>
  1071.  
  1072. #define ROWS 5
  1073. #define COLS 10
  1074.  
  1075. int multi[ROWS][COLS];
  1076.  
  1077. int main(void)
  1078. {
  1079.   int row, col;
  1080.   for (row = 0; row < ROWS; row++)
  1081.     for(col = 0; col < COLS; col++)
  1082.       multi[row][col] = row*col;
  1083.   for (row = 0; row < ROWS; row++)
  1084.     for(col = 0; col < COLS; col++)
  1085.     {
  1086.       printf("\n%d  ",multi[row][col]);
  1087.       printf("%d ",*(*(multi + row) + col));
  1088.     }
  1089.   return 0;
  1090. }
  1091. ----------------- end of program 6.1 ---------------------
  1092.  
  1093.     Because of the double de-referencing required in the pointer
  1094. version, the name of a 2 dimensional array is said to be a
  1095. pointer to a pointer.  With a three dimensional array we would be
  1096. dealing with an array of arrays of arrays and a pointer to a
  1097. pointer to a pointer.  Note, however, that here we have initially
  1098. set aside the block of memory for the array by defining it using
  1099. array notation.  Hence, we are dealing with an constant, not a
  1100. variable.  That is we are talking about a fixed pointer not a
  1101. variable pointer.  The dereferencing function used above permits
  1102. us to access any element in the array of arrays without the need
  1103. of changing the value of that pointer (the address of multi[0][0]
  1104. as given by the symbol "multi").
  1105.  
  1106. EPILOG:
  1107.  
  1108.     I have written the preceding material to provide an
  1109. introduction to pointers for newcomers to C.  In C, the more one
  1110. understands about pointers the greater flexibility one has in the
  1111. writing of code.  The above has just scratched the surface of the
  1112. subject. In time I hope to expand on this material.  Therefore,
  1113. if you have questions, comments, criticisms, etc. concerning that
  1114. which has been presented, I would greatly appreciate your
  1115. contacting me using one of the mail addresses cited in the
  1116. Introduction.
  1117.  
  1118. Ted Jensen
  1119. .D 1 273
  1120.