"c Language Review Tour" second: pointer detailed step

Source: Internet
Author: User

Description

The first article reviews the basic concepts of pointers and their basic use, so there is a clearer idea of pointers, but in fact the first article on pointers is not easy to forget. This is the second part of the content is relatively easy to confuse, but for pointers to further learning is also very important.



I. Pointers to functions


1. Function pointers


• The function pointer is a pointer to the function, and the function pointer value is the entry address of the function, which can be used by using the pointer;

• Write a program that returns the maximum value of two numbers, calling the function through a function pointer:

The A.main function code is as follows:

#include <stdio.h>int max (int *, int *); int main (void) {int A, b, max_num;  int *pa, *PB;  Int (*p) (int *, int *);  printf ("Please input A:");  scanf ("%d", &a);p a = &a;  printf ("Please input B:");  scanf ("%d", &b);p B = &b;  p = max;  Let P point to max Funtion. Max_num = (*p) (PA, Pb);  Call the funtion through pointer p.//max_num = MAX (PA, Pb);  printf ("The max number is:%d\n", max_num); return 0;}

The B.max function code is as follows:

#include <stdio.h>int max (int *pa, int *pb) {if (*pa >= *PB) return *PA; else return *PB;}

C. The implementation process is as follows:

[Email protected]:~/stuc/fun$ gcc-c max.c max_fun.c[email protected]:~/stuc/fun$ gcc-o Max max.o Max_fun.o[email Protect ed]:~/stuc/fun$./max Please input a:3please input b:4the max number Is:4


• By the above program, define the function pointer method as:

Int (*p) (int *, int *) type name (* pointer variable name) (function argument list);

• Use a function pointer as follows:

p = max;    ===> Assignment: Assign the function entry address to the function pointer max_num = (*P) (PA, Pb); ===> Call: Change the original direct Max function call to (*P)



2. Using function pointers as function parameters


• The important role of using function pointers is to pass the address of the function as a parameter to other functions;

• Write a program that uses function pointers as function parameters to allow the user to choose to perform different functions:

The code for the A.main function is as follows:

#include <stdio.h>int sum (int *, int *); Int max (int *, int *); int  min (int *, int *); Int main (void) {  int a, b, choice, * pa, *pb;  printf ("Enter two number and choose what do you  want to do.\n ");   printf (" Please enter a: "); scanf ("%d ",  &a);p a  = &a;  printf ("please enter b:"); scanf ("%d",  &b);p b =  &b;  printf ("what do you want to do?\n");   printf ("1.Add  two number.\n2. Return the max.\n3. Return the min.\n "); &NBSP;&NBSP;SCANF ("%d ", &choice);   if (choice == 1)      fun (SUM,&NBSP;PA,&NBSP;PB);   else if (choice == 2)      fun (MAX,&NBSP;PA,&NBSP;PB);   else if (choice == 3)     fun (MIN,&NBSP;PA,&NBSP;PB);   return 0;} 

B. Call the fun function Code of the other child functions as follows:

#include <stdio.h>void fun (int (*p) (int *, int *), int *pa, int *pb) {int result;  result = (*p) (PA, Pb); printf ("The result is:%d\n", result);}

C. Return and the SUM function code is as follows:

#include <stdio.h>int sum (int *pa, int *pb) {return *PA+*PB;}

D. The max function code that returns the maximum value is as follows:

#include <stdio.h>int max (int *pa, int *pb) {if (*pa >= *PB) return *PA; else return *PB;}

E. The Min function code that returns the minimum value is as follows:

#include <stdio.h>int min (int *pa, int *pb) {if (*pa <= *PB) return *PA; else return *PB;}

F. The implementation process is as follows:

[EMAIL&NBSP;PROTECTED]:~/STUC/FUN$&NBSP;GCC&NBSP;-C&NBSP;MIX.C&NBSP;FUN.C&NBSP;SUM.C&NBSP;MAX.C&NBSP;MIN.C  [email protected]:~/stuc/fun$ gcc -o mix mix.o fun.o sum.o max.o  min.o[email protected]:~/stuc/fun$ ./mixEnter two number and choose  What do you want to do. Please enter a:3please enter b:4what do you want to do?1.add  two number.2.return the max.3.return the min.1the result is:7[email  protected]:~/stuc/fun$ ./mixEnter two number and choose what do  You want to do. Please enter a:3please enter b:4what do you want to do?1.add  two number.2.return the max.3.return the min.2the result is:4[email  protected]:~/stuc/fun$ ./mixenter two nuMber and choose what do you want to do. Please enter a:3please enter b:4what do you want to do?1.add  two number.2.return the max.3.return the min.3the result is:3



Two. Functions that return pointer values


1. Pointer functions


• Pointer functions are functions that return values as pointers;

• Rewrite the first program in the function pointer so that the return value of the function is a pointer:

The A.main function code is as follows:

#include <stdio.h>int *max (int *, int *); int main (void) {int A, B;  int *pa, *PB, *pmax;  int * (*P) (int *, int *);  printf ("Please input A:");  scanf ("%d", &a);p a = &a;  printf ("Please input B:");  scanf ("%d", &b);p B = &b;  p = max;  Let P point to max Funtion. Pmax = (*p) (PA, Pb);  Call the funtion through pointer p.//pmax = MAX (PA, Pb);  printf ("The max number is:%d\n", *pmax); return 0;}

B. The return maximum function code is as follows:

#include <stdio.h>int *max (int *pa, int *pb) {if (*pa >= *PB) return PA; else return PB;}

C. The implementation process is as follows:

[Email protected]:~/stuc/fun$ gcc-c max.c max_fun.c [email protected]:~/stuc/fun$ gcc-o Max MAX.O max_fun.o[email Protec ted]:~/stuc/fun$./maxplease input A:3please input b:4the max number Is:4


• As an indication, the return pointer value function is in the form:

int *max (int *, int *); type name * Function name (parameter list);

• Define a function pointer to a pointer value function as:

int * (*P) (int *, int *); type name * (* pointer name) (parameter list);



Three. Pointer arrays and multiple pointers


1. Array of pointers


• An array of pointer values is an array of pointers;

• Write a program that holds the title by an array of pointers:

The A.main function code is as follows:

#include <stdio.h>int main (void) {int i;                Char *book[] = {"Python", "C", "Linux", "Centos",  "Ubuntu"};  for (i = 0;i < 5;i++) printf ("%s\n", Book[i]); return 0;}

B. The implementation process is as follows:

[Email protected]:~/stuc/arry$ gcc-c arry.c [email protected]:~/stuc/arry$ gcc-o arry_book arry.o [email protected]:~/st uc/arry$./arry_book Pythonclinuxcentosubuntu


• For ease of understanding, use the following visualization:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/1D/wKiom1X1MqLxI57NAADBE7bbQ-w498.jpg "title=" Pointer array. png "alt=" wkiom1x1mqlxi57naadbe7bbq-w498.jpg "/>


• As an indication, the array of pointers is defined in the form:

Char *book[10]; type name * array name [array length];

• Note the difference from pointing to a two-dimensional array pointer:

int (*p) [4]; ===> Pointer to a one-dimensional array containing 4 elements

• Memory can be distinguished by the priority of the symbol, [] precedence is higher than *, pointer array [] is first combined, is an array, the element value is a pointer, a pointer to a two-dimensional array, * First combined, is a pointer, pointing to a one-dimensional array containing 4 elements;



2. Pointers to pointer data


• Since the processing of arrays is handled by pointers during compilation, each of the above programs is changed to read as follows:

printf ("%s\n", Book[i]); instead: printf ("%s\n", * (Book+i));

• This is consistent with the normal handling of one-dimensional arrays, except that the array is also a pointer, and there may be a confusion that can be understood by the following graphic image:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/1D/wKiom1X1N5OzEWrvAAE2Kexbb9I279.jpg "title=" Multiple pointers. png "alt=" wkiom1x1n5ozewrvaae2kexbb9i279.jpg "/>

• Can be further simplified to:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/1D/wKiom1X1OezSSTXVAABJ0K6oTs8805.jpg "title=" Multiple pointers simplify. png "alt=" wkiom1x1oezsstxvaabj0k6ots8805.jpg "/>

• The above form is: pointer to pointer (first address of the string)--the first character of the string, but is a multi-pointer;


< not finished,continue>

This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1694352

"c Language Review Tour" second: pointer detailed step

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.