5.2 pointer and function parameters (c)

Source: Internet
Author: User

Pointer and function parameters

-- CProgramDesign Language (version 2nd-New Version)"5.2 

 

The C language transmits the parameter value to the called function by passing the value. Therefore, the called function cannot directly modify the value of the variable in the main function. For example, the sorting function may use a function named swap to exchange two elements in reverse order. However, if the swap function is defined as follows:


Void Swap ( Int X, Int Y) /* Wrong */
{
Int Temp;

Temp = X;
X = Y;
Y = Temp;
}

The following statements cannot achieve this purpose.

Swap (A, B );

This is because the parameter passing adopts the value passing method, so the above swap function will not affect the values of parameters A and B in the routine that calls it. This function only exchanges the values of copies of A and B.

Then, how can we achieve our goal? The main program can pass the pointer to the variable to be exchanged to the called function, that is:

Swap (& A, & B );

Since the unary operator is used to get the address of a variable, & A is a pointer to variable. All parameters of the SWAp function are declared as pointers, and these pointers are used to indirectly access the operands they direct.


Void Swap ( Int   * Px, Int   * PY) /* Interchange * PX and * py */
{
Int Temp;

Temp= *Px;
*Px= *PY;
*PY=Temp;
}

 

The pointer parameter allows the called function to access and modify the object values in the main function. Let's look at an example: the getint function accepts input in free format and performs conversion. It splits the input callback stream into integers and generates an integer each time it is called. Getint must return the converted integer and return the end mark of the file when the input ends. These values must be returned in different ways. EOF can be expressed by any value or an input integer.

You can design this function as follows: It determines whether the State at the end of the file is returned by the getint function. At the same time, it uses a pointer parameter to store the converted integer and return it to the main function. The implementation of the function scanf adopts this method.

The following loop statement calls the getint function to assign a value to an integer array:

Int N, array [size], getint (int *);

For (n = 0; n <size & getint (& array [N])! = EOF; n ++)

Each time getint is called, the next integer in the input stream is assigned to the array element array [0], and the value of N is increased by 1. Note that the address of array [N] must be passed to the getint function. Otherwise, the getint function cannot return the converted integer to the caller.

The getint function of this version returns EOF when it reaches the end of the file. If the next input is not a number, 0 is returned. If the input contains a meaningful number, a positive value is returned.


# Include < Ctype. h >

IntGetch (Void);
VoidUngetch (Int);

/* Getint: Get next integer from input into * PN */
Int Getint ( Int   * PN)
{
Int C, sign;
While (Isspace (C = Getchar ())) /* 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
}

In the getint function, * PN is always used as a common integer variable. The getch and ungetch functions are also used. With these two functions, the getint function can re-write a redundant character that must be read into the input.

/*********************************

* Last edit by old at 2009.12.26

********************************/ 

C programming language version 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.