Author: talking dumb
Recently, there are many friends who learn C asks me some usage of the pointer, and now you will explain it through a program.
In the program I defined several sub-functions, and through the respective temporary variables, it indicates a certain use of the pointer, I hope to give you guidance!
.
Below is the source program:
#include
#include
#include
#include
Void PointArray ();
Void arraypoint ();
Long * max (long A, long b);
Void pointfun ();
Void funpoint ();
void dobpoint ();
Void voidpoint ();
Void Juge (INT ChOIC);
int main ()
{
Unsigned int kho;
Printf ("Training of Pointer / N");
Printf ("Written By Talkingmute / N);
Printf ("QQ: 13946698 / N / N");
DO
{
Printf ("/ n pointer exercise / N");
Printf ("1 - pointer array / n");
Printf ("2-- array pointer / n");
Printf ("3 - pointer function / n");
Printf ("4 - function pointer / N");
Printf ("5 - pointer / n");
Printf ("6 - Void Type Pointer / N");
Printf ("7 - exit / n");
Printf ("Please enter your selection (1-7):");
Scanf ("% U", & choice);
Juge (choheice);
Getch ();
}
While (choice> = 1 && choice <= 7);
Return 0;
}
Void Juge (int Choic)
{
Switch (chof)
{
Case 1:
PointArray (); Break;
Case 2:
ArrayPoint (); BREAK;
Case 3:
PointFun (); Break;
Case 4:
Funpoint (); Break;
Case 5:
DobPoint (); BREAK;
Case 6:
VoidPoint (); BREAK;
Case 7:
exit (0);
DEFAULT:
Printf ("Input Error / N");
}
}
Void pointarray ()
{
Char * p [4] = {"Hello", "World", "Rapture", "City"};
Printf ("char * p [4] = {Hello, World, Rapture, City} / n");
Printf ("Enter the value of each pointer in the form of an array element: / n");
Getch ();
For (unsigned int = 0; i <4; i )
Printf ("p [% u] =% s / t", i, p [i]);
Printf ("/ n");
}
void arraypoint ()
{
Int a [] [3] = {9, 10, 11, 15, 16, 17};
INT (* P) [3];
P = a;
Printf ("INT A [] [3] = {9, 10, 11, 15, 16, 17}; / n");
Printf ("INT (* P) [3]; P = a; / n"); Printf ("" "" Use an array pointer P to output all values: / n ");
Getch ();
For (unsigned int = 0; i <2; i )
{
For (unsigned int j = 0; j <3; j )
Printf ("* (* (p % U) % U) =% D / T", I, J, * (* (p i) j));
Printf ("/ n");
}
}
Long * max (long A, long b)
{
Long * temp;
IF (a> b)
Temp = & a;
Else
Temp = & b;
Return Temp;
}
Void pointfun ()
{
Printf ("long * max (long a, long b) / n");
Long AT, BT, C;
Long * p;
Printf ("Input A:");
Scanf ("% d", & at);
Printf ("Enter B:");
Scanf ("% D", & bt);
P = Max (at, bt);
Printf ("long * p; p = max (long a, long b);");
Printf ("(" The pointer variable returned by the function is assigned to the pointer variable p.) / n ");
Getch ();
Printf ("* p =% ld / t", * p);
Printf ("(* p) =% ld", (* p) );
Printf ("/ n");
}
Void funpoint ()
{
INT X;
Printf ("INT ABS (INT X) / N");
INT (INT X);
PO = ABS;
Printf ("INT X); PO = ABS;");
Printf ("(" function of the function is assigned to the pointer variable PO.) / n ");
Printf ("Enter x:");
Scanf ("% D", & x);
PRINTF ("PO (X) =% D / N", PO (x));
Printf ("(* PO) (x) =% D", (* PO) (x));
Printf ("/ n");
}
void dobpoint ()
{
INT I;
INT * T;
Int ** P;
Printf ("INT I; INT * T; INT ** P; / N");
Printf ("Enter i:");
Scanf ("% D", & i);
T = & I;
P = & t;
Printf ("t = & i; p = & t; (p is a pointer pointing pointer variable t) / n");
Getch ();
Printf ("* t =% D, ** P =% D, & t =% # x, p =% # x", i, i, & t, p);
Printf ("/ n");
}
Void voidpoint ()
{
INT * NP;
Int Num;
Printf ("Void * Calloc (unsigned num, unsigned size / n");
Printf ("Enter Num:");
Scanf ("% D", & num);
Np = (int *) Calloc (NUM, SIZEOF (INT)); Printf ("INT * NP; NP = (INT *) Calloc (NUM, SIZEOF (INT)); / N");
Printf ("(" (Void pointer needs to be used to convert.) / n ");
Getch ();
Printf ("INT NP [% D] = {", NUM);
For (int i = 0; i { NP [i] = i * 2; Printf ("% D,", NP [i]); } Printf ("} / n"); Free (np); }