• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

ProgEx06

Started by Frederick J. Harris, November 04, 2009, 04:37:21 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/* ProxEx06

   This program shows the use of pointers in passing parameters to functions.  Using this
   technique a function receives the address of a variable defined somewhere else, and
   because it has the address of the variable, it can modify it (if it isn't declared as
   (const - another topic!).  Note in the program below in main() an int named iNumber is
   defined.  In the declaration it is set equal to five.  The first printf statement outputs
   that value.  The second printf statement uses C's 'address of' operator - & - to output
   the address where iNumber is stored.  This operator is used by prepending it to the
   front of a variable.  Then the function IncrOne() is called, and the address of iNumber
   is passed to it. Note that the prototype of the function IncrOne requires a pointer as
   a parameter.  Well, a pointer to an int could have been declared in main like so...

   int* pNumber;

   ...and then this could have been done...

   pNumber = &iNumber;

   ...and then the function called like so...


   printf("IncrOne(5)=%d\n",IncrOne(pNumber));

   But, since pNumber = &iNumber, it avoids an unnecessary variable declaration to just do
   as in the program...

   printf("IncrOne(5)=%d\n",IncrOne(&iNumber));

   When the above statement executes, and before it returns, the next output statement
   prints the address held in the pointer 'ptrNumber'.  It is the same number as the address
   of iNumber back in main(), i.e., 2293620.  One way to read the confusing C statement
   return ++*ptrNumber would be 'Increment by one whatever quantity is stored at 2,293,620,
   and return to the calling routine that incremented number'.  The function IncrOne is
   declared as returning an integer, and it returns to main the number six, which is one more
   than the number passed to it.  Also note that an output statement right before the getchar()
   outputs the value of iNumber, which is now six.  If you compare this result with what
   happened in ProgEx05 you'll recall that after the call to IncrOne iNumber still equaled
   five back there and passing iNumber to the function did not cause it to be incremented
   in main().  In this case the function IncrOne returned one more that the arguement passed
   to it, but it did this by incrementing the arguement itself, which was not the case with
   ProgEx05.  The conclusion to be drawn here is that when you pass a pointer to a function,
   the function can modify the value of its parameter because it is actually working directly
   with the address of a variable declared back in the calling procedure.  This is sometimes
   called 'pass by reference', although we will define that term a little closer in the next
   example.

   Whenever you see something like *ptrNumber in a program (other than in the variable
   declaration), what is being referred to is the number stored at the address held in
   ptrNumber.  So in this case, after the function call, ptrNumber is equal to the address
   of variable iNumber, i.e., 2293620, and *ptrNumber (what's stored at ptrNumber) is equal
   to six.  This operation is referred to as 'dereferencing the pointer'.

   Just to add to the confusion it might be noted that the variable ptrNumber also must be
   stored somewhere (afterall, its a variable too), so this quantity '&ptrNumber' also has
   a rather definite existance!!!  But relax!  We won't get into that right now!
*/

#include <stdio.h>

int IncrOne(int* ptrNumber)
{
printf("(In Function IncrOne()) -- ptrNumber=%u\n",(unsigned int)ptrNumber);
return ++*ptrNumber;                            /* prints address passed to function                  */
}

int main(void)
{
int iNumber=5;

printf("iNumber=%d\n",iNumber);                 /* prints value stored in variable iNumber             */
printf("&iNumber=%u\n",(unsigned int)&iNumber); /* prints address where iNumber is stored              */
printf("IncrOne(5)=%d\n",IncrOne(&iNumber));    /* passes address of iNumber to function               */
printf("iNumber=%d\n",iNumber);                 /* prints value stored in variable iNumber             */
getchar();                                      /* pauses execution by waiting for character key press */

return 0;
}

/*
Output:
============================================
iNumber=5
&iNumber=2293620
(In Function IncrOne()) -- ptrNumber=2293620
IncrOne(5)=6
iNumber=6
*/