home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / C / PTRTUT / CH4X.HTM < prev    next >
Text File  |  1997-07-19  |  4KB  |  158 lines

  1. <H2>CHAPTER 4: More on Strings</H2>
  2.  
  3.  
  4.  
  5.     Well, we have progressed quite a way in a short time!  Let's
  6. back up a little and look at what was done in Chapter 3 on
  7. copying of strings but in a different light.  Consider the
  8. following function:
  9.  
  10. <PRE>
  11.     char *my_strcpy(char dest[], char source[])
  12.     {
  13.         int i = 0;
  14.         while (source[i] != '\0')
  15.         {
  16.             dest[i] = source[i];
  17.             i++;
  18.         }
  19.         dest[i] = '\0';
  20.         return dest;
  21.     }
  22. </PRE>
  23.  
  24.     Recall that strings are arrays of characters.  Here we have
  25. chosen to use array notation instead of pointer notation to do
  26. the actual copying.  The results are the same, i.e. the string
  27. gets copied using this notation just as accurately as it did
  28. before.  This raises some interesting points which we will
  29. discuss.
  30.  
  31. <P>
  32.     Since parameters are passed by value, in both the passing of
  33. a character pointer or the name of the array as above, what
  34. actually gets passed is the address of the first element of each
  35. array.  Thus, the numerical value of the parameter passed is the
  36. same whether we use a character pointer or an array name as a
  37. parameter.  This would tend to imply that somehow<B> source[i]</B>
  38. is the same as <B>*(p+i)</B>.
  39. <P>
  40.     In fact, this is true, i.e wherever one writes <B>a[i]</B>  it can
  41. be replaced with  <B>*(a + i)</B> without any problems.  In fact, the
  42. compiler will create the same code in either case.  Thus we see
  43. that pointer arithmetic is the same thing as array indexing.
  44. Either syntax produces the same result.
  45.  
  46. <P>
  47.  
  48.     This is NOT saying that pointers and arrays are the same
  49. thing, they are not.  We are only saying that to identify a given
  50. element of an array we have the choice of two syntaxes, one using
  51. array indexing and the other using pointer arithmetic, which
  52. yield identical results.
  53.  
  54. <P>
  55.  
  56.     Now, looking at this last expression, part of it..  <B>(a + i)</B>,
  57. is a simple addition using the <B>+</B> operator and the rules of C
  58. state that such an expression is commutative.  That is  <B>(a + i)</B>
  59. is identical to <B>(i + a)</B>.  Thus we could write <B>*(i + a)</B> just as
  60. easily as <B>*(a + i)</B>.
  61.  
  62. <P>
  63.  
  64.     But <B>*(i + a)</B> could have come from <B>i[a]</B> !  From all of this
  65. comes the curious truth that if:
  66.  
  67. <PRE>
  68.     char a[20];
  69.     int i;
  70. </PRE>
  71.    writing
  72. <PRE>
  73.     a[3] = 'x';
  74. </PRE>
  75.     is the same as writing
  76. <PRE>
  77.     3[a] = 'x';
  78. </PRE>
  79.  
  80.     Try it!  Set up an array of characters, integers or longs,
  81. etc. and assigned the 3rd or 4th element a value using the
  82. conventional approach and then print out that value to be sure
  83. you have that working.  Then reverse the array notation as I have
  84. done above.  A good compiler will not balk and the results will
  85. be identical.  A curiosity... nothing more!
  86.  
  87. <P>
  88.  
  89.     Now, looking at our function above, when we write:
  90.  
  91. <PRE>
  92.     dest[i] = source[i];
  93. </PRE>
  94.  
  95. due to the fact that array indexing and pointer arithmetic yield
  96. identical results, we can write this as:
  97.  
  98. <PRE>
  99.     *(dest + i) = *(source + i);
  100. </PRE>
  101.  
  102.     But, this takes 2 additions for each value taken on by i.
  103. Additions, generally speaking, take more time than
  104. incrementations (such as those done using the <B>++</B> operator as in
  105. <B>i++</B>).  This may not be true in modern optimizing compilers, but
  106. one can never be sure.  Thus, the pointer version may be a bit
  107. faster than the array version.
  108.  
  109. <P>
  110.  
  111.     Another way to speed up the pointer version would be to
  112. change:
  113.  
  114. <PRE>
  115.     while (*source != '\0')
  116. </PRE>
  117.  
  118.     to simply
  119.  
  120. <PRE>
  121.     while (*source)
  122. </PRE>
  123.  
  124. since the value within the parenthesis will go to zero (FALSE) at
  125. the same time in either case.
  126.  
  127. <P>
  128.  
  129.     At this point you might want to experiment a bit with writing
  130. some of your own programs using pointers.  Manipulating strings
  131. is a good place to experiment.  You might want to write your own
  132. versions of such standard functions as:
  133.  
  134. <PRE>
  135.     strlen();
  136.     strcat();
  137.     strchr();
  138. </PRE>
  139.  
  140. and any others you might have on your system.
  141.  
  142. <P>
  143.  
  144.     We will come back to strings and their manipulation through
  145. pointers in a future chapter.  For now, let's move on and discuss
  146. structures for a bit.
  147.  
  148. <DL>
  149.  
  150. <DT><Center><A HREF=ch5x.htm>Continue with Pointer Tutorial</A></Center>
  151.  
  152. </DT></P>
  153.  
  154. <DT><Center><A Href=pointers.htm>Back to Table of Contents</Center></A><P>
  155.  
  156. </DL>
  157.  
  158.