home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP8.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  19.1 KB  |  455 lines

  1.                             Chapter 8 - Pointers
  2.  
  3.  
  4.                              WHAT IS A POINTER?
  5.  
  6.              Simply  stated,  a pointer is an address.   Instead  of 
  7.  
  8.         being  a  variable,  it  is a pointer to a  variable  stored 
  9.  
  10.         somewhere in the address space of the program.  It is always 
  11.  
  12.         best to use an example so load the file named POINTER.C  and 
  13.  
  14.         display  it on your monitor for an example of a program with 
  15.  
  16.         some pointers in it.
  17.  
  18.              For  the moment,  ignore the data declaration statement 
  19.  
  20.         where we define "index" and two other fields beginning  with 
  21.  
  22.         a star.   It is properly called an asterisk, but for reasons 
  23.  
  24.         we  will see later,  let's agree to call it a star.   If you 
  25.  
  26.         observe  the  first statement,  it should be clear  that  we 
  27.  
  28.         assign the value of 39 to the variable "index".   This is no 
  29.  
  30.         surprise,  we  have been doing it for several programs  now.  
  31.  
  32.         The  next  statement  however,  says to assign  to  "pt1"  a 
  33.  
  34.         strange looking value,  namely the variable "index" with  an 
  35.  
  36.         ampersand in front of it.   In this example, pt1 and pt2 are 
  37.  
  38.         pointers,  and  the  variable "index" is a simple  variable.  
  39.  
  40.         Now we have a problem.  We need to learn how to use pointers 
  41.  
  42.         in a program, but to do so requires that first we define the 
  43.  
  44.         means of using the pointers in the program.
  45.  
  46.              The  following two rules will be somewhat confusing  to 
  47.  
  48.         you at first but we need to state the definitions before  we 
  49.  
  50.         can  use  them.   Take your time,  and the whole thing  will 
  51.  
  52.         clear up very quickly.
  53.  
  54.                           TWO VERY IMPORTANT RULES
  55.  
  56.              The  following two rules are very important when  using 
  57.  
  58.         pointers and must be thoroughly understood.
  59.  
  60.         1.  A variable name with an ampersand in front of it defines 
  61.  
  62.             the  address of the variable and therefore points to the 
  63.  
  64.             variable.   You  can therefore read line six as  "pt1 is 
  65.  
  66.             assigned the value of the address of index". 
  67.            
  68.         2.  A  pointer  with a "star" in front of it refers  to  the 
  69.  
  70.             value of the variable pointed to by the  pointer.   Line 
  71.  
  72.             nine of the program can be read as "The stored (starred) 
  73.  
  74.             value  to which the pointer "pt1" points is assigned the 
  75.  
  76.             value  13".   Now  you can see why it is  convenient  to 
  77.  
  78.             think of the asterisk as a star,  it sort of sounds like 
  79.  
  80.             the word store.
  81.  
  82.                                   MEMORY AIDS
  83.  
  84.             1. Think of & as an address.
  85.             2. Think of * as a star referring to stored.
  86.  
  87.  
  88.  
  89.                                   Page 48
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.                             Chapter 8 - Pointers
  100.  
  101.  
  102.  
  103.              Assume for the moment that "pt1" and "pt2" are pointers 
  104.  
  105.         (we will see how to define them shortly).  As pointers, they 
  106.  
  107.         do not contain a variable value but an address of a variable 
  108.  
  109.         and  can be used to point to a variable.   Line six  of  the 
  110.  
  111.         program  assigns the pointer "pt1" to point to the  variable 
  112.  
  113.         we have already defined as "index" because we have  assigned 
  114.  
  115.         the address of "index" to "pt1".  Since we have a pointer to 
  116.  
  117.         "index",  we  can  manipulate the value of "index" by  using 
  118.  
  119.         either the variable name itself, or the pointer.
  120.  
  121.              Line  nine  modifies the value by  using  the  pointer. 
  122.  
  123.         Since the pointer "pt1" points to the variable "index", then 
  124.  
  125.         putting  a  star in front of the pointer name refers to  the 
  126.  
  127.         memory  location  to  which  it  is  pointing.    Line  nine 
  128.  
  129.         therefore  assigns to "index" the value of 13.  Anyplace  in 
  130.  
  131.         the program where it is permissible to use the variable name 
  132.  
  133.         "index", it is also permissible to use the name "*pt1" since 
  134.  
  135.         they   are  identical  in  meaning  until  the  pointer   is 
  136.  
  137.         reassigned to some other variable.
  138.  
  139.                               ANOTHER POINTER
  140.  
  141.              Just  to add a little intrigue to the system,  we  have 
  142.  
  143.         another  pointer defined in  this  program,  "pt2".    Since 
  144.  
  145.         "pt2"  has  not  been assigned a value  prior  to  statement 
  146.  
  147.         seven,  it  doesn't point to anything,  it contains garbage.  
  148.  
  149.         Of course,  that is also true of any variable until a  value 
  150.  
  151.         is  assigned to it.  Statement seven assigns "pt2" the  same 
  152.  
  153.         address  as  "pt1",  so  that now "pt2" also points  to  the 
  154.  
  155.         variable  "index".   So to continue the definition from  the 
  156.  
  157.         last  paragraph,   anyplace  in  the  program  where  it  is 
  158.  
  159.         permissible  to  use  the  variable  "index",   it  is  also 
  160.  
  161.         permissible  to  use  the  name  "*pt2"  because  they   are 
  162.  
  163.         identical in meaning.  This fact is illustrated in the first 
  164.  
  165.         "printf" statement since this statement uses the three means 
  166.  
  167.         of  identifying  the  same variable to print  out  the  same 
  168.  
  169.         variable three times.
  170.  
  171.                          THERE IS ONLY ONE VARIABLE
  172.  
  173.              Note carefully that,  even though it appears that there 
  174.  
  175.         are three variables, there is really only one variable.  The 
  176.  
  177.         two  pointers  point  to  the  single  variable.    This  is 
  178.  
  179.         illustrated in the next statement which assigns the value of 
  180.  
  181.         13  to  the  variable "index",  because that  is  where  the 
  182.  
  183.         pointer  "pt1"  is pointing.   The next  "printf"  statement 
  184.  
  185.         causes  the  new value of 13 to be printed out three  times.  
  186.  
  187.         Keep  in mind that there is really only one variable  to  be 
  188.  
  189.         changed, not three.
  190.  
  191.  
  192.  
  193.  
  194.                                   Page 49
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.                             Chapter 8 - Pointers
  205.  
  206.  
  207.              This is admittedly a very difficult concept,  but since 
  208.  
  209.         it  is  used  extensively  in all but  the  most  trivial  C 
  210.  
  211.         programs,  it  is  well  worth your time to stay  with  this 
  212.  
  213.         material until you understand it thoroughly.
  214.  
  215.                         HOW DO YOU DECLARE A POINTER?
  216.  
  217.              Now  to  keep a promise and tell you how to  declare  a 
  218.  
  219.         pointer.   Refer  to the third line of the program  and  you 
  220.  
  221.         will  see  our  old familiar way of  defining  the  variable 
  222.  
  223.         "index",  followed  by  two more  definitions.   The  second 
  224.  
  225.         definition  can  be read as "the storage location  to  which 
  226.  
  227.         "pt1"  points  will be an int  type  variable".   Therefore, 
  228.  
  229.         "pt1" is a pointer to an int type variable.  Likewise, "pt2" 
  230.  
  231.         is another pointer to an int type variable. 
  232.  
  233.              A  pointer  must  be defined to point to some  type  of 
  234.  
  235.         variable.   Following a proper definition, it cannot be used 
  236.  
  237.         to point to any other type of variable or it will result  in 
  238.  
  239.         a  "type incompatibility" error.   In the same manner that a 
  240.  
  241.         "float"  type of variable cannot be added to an  "int"  type 
  242.  
  243.         variable,  a pointer to a "float" variable cannot be used to 
  244.  
  245.         point to an integer variable. 
  246.  
  247.              Compile and run this program and observe that there  is 
  248.  
  249.         only one variable and the single statement in line 9 changes 
  250.  
  251.         the one variable which is displayed three times.
  252.  
  253.                       THE SECOND PROGRAM WITH POINTERS
  254.  
  255.              In these few pages so far on pointers,  we have covered 
  256.  
  257.         a lot of territory, but it is important territory.  We still 
  258.  
  259.         have  a  lot  of  material to cover so stay in  tune  as  we 
  260.  
  261.         continue  this important aspect of C.   Load the  next  file 
  262.  
  263.         named  POINTER2.C  and display it on your monitor so we  can 
  264.  
  265.         continue our study.
  266.  
  267.              In  this program we have defined several variables  and 
  268.  
  269.         two pointers.   The first pointer named "there" is a pointer 
  270.  
  271.         to  a "char" type variable and the second named "pt"  points 
  272.  
  273.         to an "int" type variable.  Notice also that we have defined 
  274.  
  275.         two  array variables named "strg" and "list".   We will  use 
  276.  
  277.         them  to show the correspondence between pointers and  array 
  278.  
  279.         names.
  280.  
  281.                   A STRING VARIABLE IS ACTUALLY A POINTER
  282.  
  283.              In  the programming language C,  a string  variable  is 
  284.  
  285.         defined to be simply a pointer to the beginning of a string.  
  286.  
  287.         This  will  take  some explaining.   Refer  to  the  example 
  288.  
  289.         program  on  your monitor.   You will notice that  first  we 
  290.  
  291.  
  292.  
  293.                                   Page 50
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.                             Chapter 8 - Pointers
  304.  
  305.  
  306.         assign a string constant to the string variable named "strg" 
  307.  
  308.         so we will have some data to work with.  Next, we assign the 
  309.  
  310.         value  of the first element to the variable "one",  a simple 
  311.  
  312.         "char" variable.   Next,  since the string name is a pointer 
  313.  
  314.         by  definition  of the C language,  we can assign  the  same 
  315.  
  316.         value to "two" by using the star and the string  name.   The 
  317.  
  318.         result  of  the two assignments are such that "one" now  has 
  319.  
  320.         the same value as "two", and both contain the character "T", 
  321.  
  322.         the  first character in the string.   Note that it would  be 
  323.  
  324.         incorrect  to  write  the ninth line as  "two  =  *strg[0];" 
  325.  
  326.         because the star takes the place of the square brackets.
  327.  
  328.              For all practical purposes,  "strg" is a  pointer.   It 
  329.  
  330.         does, however, have one restriction that a true pointer does 
  331.  
  332.         not  have.   It cannot be changed like a variable,  but must 
  333.  
  334.         always contain the initial value and therefore always points 
  335.  
  336.         to  its  string.   It  could  be thought  of  as  a  pointer 
  337.  
  338.         constant,  and in some applications you may desire a pointer 
  339.  
  340.         that cannot be corrupted in any way.   Even though it cannot 
  341.  
  342.         be changed, it can be used to refer to other values than the 
  343.  
  344.         one  it is defined to point to,  as we will see in the  next 
  345.  
  346.         section of the program.
  347.  
  348.              Moving ahead to line 12, the variable "one" is assigned 
  349.  
  350.         the  value of the ninth variable (since the indexing  starts 
  351.  
  352.         at zero) and "two" is assigned the same value because we are 
  353.  
  354.         allowed to index a pointer to get to values farther ahead in 
  355.  
  356.         the string.  Both variables now contain the character "a".
  357.  
  358.             The C programming language takes care of indexing for us 
  359.  
  360.         automatically  by  adjusting the indexing for  the  type  of 
  361.  
  362.         variable  the  pointer is pointing to.   In this  case,  the 
  363.  
  364.         index  of  8  is simply added to the  pointer  value  before 
  365.  
  366.         looking up the desired result because a "char" type variable 
  367.  
  368.         is  one byte long.   If we were using a pointer to an  "int" 
  369.  
  370.         type variable,  the index would be doubled and added to  the 
  371.  
  372.         pointer  before  looking up the value because an "int"  type 
  373.  
  374.         variable  uses two bytes per value stored.   When we get  to 
  375.  
  376.         the chapter on structures,  we will see that a variable  can 
  377.  
  378.         have  many,   even  into  the  hundreds  or  thousands,   of 
  379.  
  380.         characters  per variable,  but the indexing will be  handled 
  381.  
  382.         automatically for us by the system.
  383.  
  384.              Since "there" is already a pointer,  it can be assigned 
  385.  
  386.         the value of the eleventh element of "strg" by the statement 
  387.  
  388.         in line 16 of the program.  Remember that since "there" is a 
  389.  
  390.         true  pointer,  it can be assigned any value as long as that 
  391.  
  392.         value  represents a "char" type of address.   It  should  be 
  393.  
  394.         clear  that  the pointers must be "typed" in order to  allow 
  395.  
  396.         the pointer arithmetic described in the last paragraph to be 
  397.  
  398.  
  399.  
  400.  
  401.  
  402.                                   Page 51
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.                             Chapter 8 - Pointers
  413.  
  414.  
  415.         done  properly.   The third and fourth outputs will  be  the 
  416.  
  417.         same, namely the letter "c".
  418.  
  419.                              POINTER ARITHMETIC
  420.  
  421.              Not  all  forms  of  arithmetic are  permissible  on  a 
  422.  
  423.         pointer.   Only  those things that make  sense,  considering 
  424.  
  425.         that a pointer is an address somewhere in the computer.   It 
  426.  
  427.         would  make sense to add a constant to an  address,  thereby 
  428.  
  429.         moving it ahead in memory that number of places.   Likewise, 
  430.  
  431.         subtraction  is permissible,  moving it back some number  of 
  432.  
  433.         locations.   Adding  two  pointers together would  not  make 
  434.  
  435.         sense  because absolute memory addresses are  not  additive.  
  436.  
  437.         Pointer multiplication is also not allowed, as that would be 
  438.  
  439.         a  funny number.   If you think about what you are  actually 
  440.  
  441.         doing,  it will make sense to you what is allowed,  and what 
  442.  
  443.         is not.    
  444.  
  445.                          NOW FOR AN INTEGER POINTER
  446.  
  447.              The  array named "list" is assigned a series of  values 
  448.  
  449.         from  100  to 199 in order to have some data to  work  with.  
  450.  
  451.         Next  we  assign  the  pointer "pt" the value  of  the  28th 
  452.  
  453.         element  of the list and print out the same value both  ways 
  454.  
  455.         to  illustrate that the system truly will adjust  the  index 
  456.  
  457.         for the "int" type variable.   You should spend some time in 
  458.  
  459.         this program until you feel you fairly well understand these 
  460.  
  461.         lessons on pointers.
  462.  
  463.              Compile and run POINTER2.C and study the output.
  464.  
  465.              You  may recall that back in the lesson on functions we 
  466.  
  467.         mentioned that there were two ways to get variable data back 
  468.  
  469.         from a function.   One way is through use of the array,  and 
  470.  
  471.         you should be right on the verge of guessing the other  way.  
  472.  
  473.         If your guess is through use of a pointer,  you are correct.  
  474.  
  475.         Load  and display the program named TWOWAY.C for an  example 
  476.  
  477.         of this.
  478.  
  479.                     FUNCTION DATA RETURN WITH A POINTER
  480.              
  481.              In  TWOWAY.C,  there  are two variables defined in  the 
  482.  
  483.         main program "pecans" and "apples".   Notice that neither of 
  484.  
  485.         these is defined as a pointer.   We assign values to both of 
  486.  
  487.         these  and print them out,  then call the  function  "fixup" 
  488.  
  489.         taking with us both of these values.   The variable "pecans" 
  490.  
  491.         is  simply  sent  to the function,  but the address  of  the 
  492.  
  493.         variable  "apples" is sent to the function.   Now we have  a 
  494.  
  495.         problem.   The two arguments are not the same, the second is 
  496.  
  497.         a pointer to a variable.  We must somehow alert the function 
  498.  
  499.         to  the  fact  that it is supposed  to  receive  an  integer 
  500.  
  501.  
  502.  
  503.                                   Page 52
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.                             Chapter 8 - Pointers
  514.  
  515.  
  516.         variable  and a pointer to an integer variable.   This turns 
  517.  
  518.         out   to  be  very  simple.    Notice  that  the   parameter 
  519.  
  520.         definitions in the function define "nuts" as an integer, and 
  521.  
  522.         "fruit"  as a pointer to an integer.   The call in the  main 
  523.  
  524.         program  therefore  is now in agreement  with  the  function 
  525.  
  526.         heading and the program interface will work just fine.
  527.  
  528.              In  the body of the function,  we print the two  values 
  529.  
  530.         sent  to  the function,  then modify them and print the  new 
  531.  
  532.         values out.   This should be perfectly clear to you by  now.  
  533.  
  534.         The  surprise occurs when we return to the main program  and 
  535.  
  536.         print out the two values again.  We will find that the value 
  537.  
  538.         of  pecans will be restored to its value before the function 
  539.  
  540.         call  because  the C language makes a copy of  the  item  in 
  541.  
  542.         question and takes the copy to the called function,  leaving 
  543.  
  544.         the original intact.   In the case of the variable "apples", 
  545.  
  546.         we  made  a copy of a pointer to the variable and  took  the 
  547.  
  548.         copy of the pointer to the function.  Since we had a pointer 
  549.  
  550.         to  the  original variable,  even though the pointer  was  a 
  551.  
  552.         copy,  we  had  access  to the original variable  and  could 
  553.  
  554.         change  it in the function.   When we returned to  the  main 
  555.  
  556.         program,  we  found  a  changed value in  "apples"  when  we 
  557.  
  558.         printed it out. 
  559.  
  560.              By  using  a pointer in a function call,  we  can  have 
  561.  
  562.         access  to the data in the function and change it in such  a 
  563.  
  564.         way  that when we return to the calling program,  we have  a 
  565.  
  566.         changed  value  of data.    It must be pointed out  however, 
  567.  
  568.         that  if you modify the value of the pointer itself  in  the 
  569.  
  570.         function,  you  will have a restored pointer when you return 
  571.  
  572.         because the pointer you use in the function is a copy of the 
  573.  
  574.         original.  In this example, there was no pointer in the main 
  575.  
  576.         program because we simply sent the address to the  function, 
  577.  
  578.         but  in  many  programs you will use  pointers  in  function 
  579.  
  580.         calls.  One of the places you will find need for pointers in 
  581.  
  582.         function  calls  will be when you request data  input  using 
  583.  
  584.         standard  input/output routines.   These will be covered  in 
  585.  
  586.         the next two chapters.
  587.  
  588.              Compile and run TWOWAY.C and observe the output.
  589.  
  590.                            POINTERS ARE VALUABLE
  591.  
  592.              Even  though  you are probably somewhat intimidated  at 
  593.  
  594.         this point by the use of pointers,  you will find that after 
  595.  
  596.         you  gain experience,  you will use them profusely  in  many 
  597.  
  598.         ways.  You will also use pointers in every program you write 
  599.  
  600.         other than the most trivial because they are so useful.  You 
  601.  
  602.         should  probably  go  over this material  carefully  several 
  603.  
  604.         times until you feel comfortable with it because it is  very 
  605.  
  606.  
  607.  
  608.  
  609.  
  610.                                   Page 53
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.                             Chapter 8 - Pointers
  621.  
  622.  
  623.         important  in the area of input/output which is next on  the 
  624.  
  625.         agenda.
  626.  
  627.  
  628.         PROGRAMMING EXERCISES
  629.  
  630.         1.   Define  a character array  and use "strcpy" to  copy  a 
  631.  
  632.              string  into it.  Print the string out by using a  loop 
  633.  
  634.              with  a  pointer to print out one character at a  time. 
  635.  
  636.              Initialize the pointer to the first element and use the 
  637.  
  638.              double  plus  sign  to increment  the  pointer.  Use  a 
  639.  
  640.              separate  integer variable to count the  characters  to 
  641.  
  642.              print.
  643.  
  644.         2.   Modify the program to print out the string backwards by 
  645.  
  646.              pointing to the end and using a decrementing pointer.
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.                                   Page 54
  685.  
  686.