Pointers and Function Arguments in C

By: Norman Chap Emailed: 1644 times Printed: 2114 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order arguments with a function called swap. It is not enough to write
   swap(a, b);
where the swap function is defined as
   void swap(int x, int y)  /* WRONG */
   {
       int temp;

       temp = x;
       x = y;
       y = temp;
   }
Because of call by value, swap can't affect the arguments a and b in the routine that called it. The function above swaps copies of a and b.

The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed:

   swap(&a, &b);
Since the operator & produces the address of a variable, &a is a pointer to a. In swap itself, the parameters are declared as pointers, and the operands are accessed indirectly through them.
   void swap(int *px, int *py)  /* interchange *px and *py */
   {
       int temp;

       temp = *px;
       *px = *py;
       *py = temp;
   }

Pointer arguments enable a function to access and change objects in the function that called it. As an example, consider a function getint that performs free-format input conversion by breaking a stream of characters into integer values, one integer per call. getint has to return the value it found and also signal end of file when there is no more input. These values have to be passed back by separate paths, for no matter what value is used for EOF, that could also be the value of an input integer.

One solution is to have getint return the end of file status as its function value, while using a pointer argument to store the converted integer back in the calling function. This is the scheme used by scanf as well;

The following loop fills an array with integers by calls to getint:

   int n, array[SIZE], getint(int *);

   for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
       ;
Each call sets array[n] to the next integer found in the input and increments n. Notice that it is essential to pass the address of array[n] to getint. Otherwise there is no way for getint to communicate the converted integer back to the caller.

Our version of getint returns EOF for end of file, zero if the next input is not a number, and a positive value if the input contains a valid number.

   #include <ctype.h>

   int getch(void);
   void ungetch(int);

   /* getint:  get next integer from input into *pn */
   int getint(int *pn)
   {
       int c, sign;

       while (isspace(c = getch()))   /* skip white space */
           ;
       if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
           ungetch(c);  /* it is not a number */
           return 0;
       }
       sign = (c == '-') ? -1 : 1;
       if (c == '+' || c == '-')
           c = getch();
       for (*pn = 0; isdigit(c), c = getch())
           *pn = 10 * *pn + (c - '0');
       *pn *= sign;
       if (c != EOF)
           ungetch(c);
       return c;
   }
Throughout getint, *pn is used as an ordinary int variable. We have also used getch and ungetch  so the one extra character that must be read can be pushed back onto the input.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Norman Chap
The break Statement example in Java
Stored Procedures example in SQL
JDBC and Tomcat context settings
File read and write - sample program in C
Pointers and Function Arguments in C
Writing the first Struts application
goto and labels in C
External Variables and Scope in C
Multiple inheritance example in C++
qsort() sample program in C++
Image and ImageItem sample program in J2ME
SimpleDateFormat sample program in Java
Observable class and the sample program in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Macro Substitution using #define in C
Find square and square root for a given number in C
goto and labels in C
Open, Creat, Close, Unlink system calls sample program in C
Using malloc() Function in C
Using realloc() Function in C
Using memset(), memcpy(), and memmove() in C
Sum of the elements of an array in C
Basics of C
Character Arrays in C
getch and ungetch in C
Example Calculator program in C - describing use of External Variables in C
Pointers and Function Arguments in C
Character Pointers and Functions in C
Table Lookup - hashtab - example program in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
Open, Creat, Close, Unlink system calls sample program in C
Unicode and UTF-8 in C
Arguments - Call by Value in C
Character Arrays in C
Initialization of Variables in C
Macro Substitution using #define in C
Pointer Arrays and Pointers to Pointers in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C
UNIX read and write system calls sample program in C
lseek() sample program in C
Formatting with printf in C
Passing double value to a function in C
Printing a simple histogram in C