Usage of pointers [2]

Source: Internet
Author: User

 

 

1.A pointer to a pointer variable is called a pointer.

Definition form: Type ** variable name

For example: int I, * p, ** q;

 

I = 30;

P = & I;

Q = & p; // pointer q points to p pointer

2.Pointers and functions

 

A. the pointer is used as a function parameter.

B. pointer Functions

C. pointer to function

 

A. Example:

Swap (int * pa, int * pb)

{

Int p;

P = * pa;

* Pa = * pb;

* Pb = p;

}

 

Main ()

{

Int a, B;

Int * p1, * p2;

Scanf ("% d", & a, & B );

P1 = & a, p2 = & B;

If (a <B)

Swap (p1, p2 );

Printf ("\ n % d, % d \ n", a, B );

}

Input from keyboard: 5 10

Output: 10, 5

Let's look at the following example.

Main ()

{

Int a, B; int * p1, * p2;

Void swap (int * pa, int * pb );

Scanf ("% d, % d, & a, & B );

P1 = & a; p2 = & B;

If (a <B)

Swap (p1, p2 );

Printf ("\ n % d, % d \ n", a, B );

}

 

Void swap (pa, pb)

{

Int * pa, * pb;

Int * p;

P = pa;

Pa = pb;

Pb = p;

}

Input from keyboard: 5 10

Output: 10, 5

Pointer function:

A function can return integer, real, and balanced values, or pointer-type data, that is, an address.

The return value is a pointer function.

The general definition form is:

Type identifier * function name (parameter table );

Example: int * a (int x, float y)

{......}

A is the function name. After calling it, you can get a pointer (Address) to the integer data)

X and y are the form parameters of function.

There is a * in front of this function, indicating that the sub-function is a pointer type function.

For example:

Main ()

{

Int a, B, * p;

Int * max (int, int );

Scanf ("% d", & a, & B );

P = max (a, B );

Printf ("max = % d", * p );

}

Int * max (int x, int y)

{If (x <y)

Return (& y );

Else return (& x );

 

Pointer to function:

The function name indicates the function entry address.

Pointer to a function: type (* pointer variable name )();

Example: int (* p )();

Example:

Int max (...)

{......}

Int (* p) (); // defines the pointer variable pointing to the Function

P = max; // enables p to point to the entry address of the max function. You can call max through (* p) ().

Example:

Main ()

{

Int max ();

Int (* p) (); // defines the function pointer

Int a, B, c;

P = max;

Scanf ("% d \ n", & a, & B );

C = (* p) (a, B); // call a function

Printf ("a = % d, B = % d, max = % d", a, B, c );

}

Int max (x, y)

{

Int z;

If (x> y) z = x;

Else z = y;

Return (z );

}

Related Article

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.