Use the difference between a string pointer variable and a character array
Both character arrays and character pointer variables enable the storage and operation of strings. But there is a difference between the two. The following issues should be noted when using:
1. The string pointer variable itself is a variable that holds the first address of the string. The string itself is stored in a contiguous memory space, headed by the first address, and ends with a ' yes ' string. Character arrays are composed of several array elements that can be used to hold the entire string.
2. An initialization assignment to an array of characters must take an external type or a static type, such as a static char st[]={"C Language"}, and no such restriction on the string pointer variable, such as: Char *ps= "C Language";
3. The string pointer method char *ps= "C Language" can be written as: Char *ps; ps= "C Language", and the array mode:
static char st[]={"C Language"};
cannot be written as:
Char st[20];st={"C Language"};
You can only assign values to individual elements of a character array.
From the above, you can see that the string pointer variable differs from the character array when it is used, but also that it is more convenient to use the pointer variable. As I said before, it's dangerous to use a pointer variable before it gets a definite address, and it's easy to cause an error. However, it is possible to assign a direct value to a pointer variable. Because the C system assigns a value to a pointer variable, it is given a definite address. So
Char *ps= "C langage";
or char *ps;
ps= "C Language"; it's all legal.
function pointer variable
In the C language, a function always occupies a contiguous memory area, and the function name is the first address of the memory area in which the function is occupied. We can assign this first address (or the entry address) of a function to a pointer variable so that the pointer variable points to the function. The function can then be found and called by the pointer variable. We refer to this pointer variable to function as "function pointer variable".
The general form of the function pointer variable definition is:
Type descriptor (* pointer variable name) ();
Where the type descriptor represents the type of the return value of the function being referred to. "(* pointer variable name)" means that the variable after "*" is a defined pointer variable. The last empty bracket indicates that the pointer variable refers to a function.
For example: Int (*PF) ();
Indicates that PF is a pointer variable that points to the entry of a function, and that the return value (function value) of the functions is an integral type.
The following example illustrates the method of implementing a function call using the pointer form.
int max (int a,int b) {
if (a>b) return A;
else return B;
}
Main () {
int max (int a,int b);
Int (*pmax) ();
int x,y,z;
Pmax=max;
printf ("Input two numbers:\n");
scanf ("%d%d", &x,&y);
Z= (*pmax) (x,y);
printf ("maxmum=%d", z);
}
From the above program can be seen with the function pointer variable form call function is as follows: 1. Define the function pointer variable, such as line 9th int (*pmax) () in the latter program, and define Pmax as a function pointer variable.
2. Give the function pointer variable the entry address (function name), such as the 11th line Pmax=max in the program;
3. Call functions with function pointer variables, such as z= (*pmax) (x,y) in the 14th line of the program; The general form of the call function is: (* pointer variable name) (the argument table) using function pointer variables should also note the following two points:
A. function pointer variables cannot perform arithmetic operations, which are different from array pointer variables. The array pointer variable adds an integer that causes the pointer to move to an array element that follows or precedes it, and the movement of the function pointer is meaningless.
B. The parentheses on either side of the "(* pointer variable name)" In a function call are not small, where the * should not be interpreted as an evaluation operation, where it is just a notation.
Pointer type function
As we mentioned earlier, the so-called function type refers to the type of the function return value. The return value of a function in the C language is a pointer (that is, an address), and the function that returns a pointer value is called a pointer type function.
The general form of defining a pointer type function is:
Type descriptor * Function name (formal parameter list)
{
.../* Function Body * *
}
The "*" number before the function name indicates that it is a pointer function, that is, the return value is a pointer. The type descriptor represents the data type that the returned pointer value points to.
Such as:
int *ap (int x,int y)
{
.../* Function Body * *
}
Indicates that the AP is a pointer-type function that returns a pointer value that points to an integer variable. The following example defines a pointer-type function day_name whose return value points to a string. A static pointer array name is defined in the function. The name array initialization assignment is eight strings, representing each weekday name and error prompt. The formal parameter n represents an integer corresponding to the name of the week. In the main function, the input integer i is used as the argument, the Day_name function is called in the printf statement and the I value is passed to the formal parameter n. The return statement in the Day_name function contains a conditional expression with an n value greater than 7 or less than 1 that returns the name[0] pointer to the main function output error prompt string "Illegal day". Otherwise, returns the weekday name of the main function output. The 7th line in the main function is a conditional statement with the semantics that aborting the program to run the exit program if entered as a negative number (i<0). Exit is a library function, exit (1) indicates that an error exits the program, and exit (0) indicates a normal exit.
What should be paid special attention to is the difference between the function pointer variable and the pointer type function in both writing and meaning. such as int (*p) () and int *p () are two completely different quantities. Int (*p) () is a variable description, stating that p is a pointer variable that points to the entry of a function, and that the return value of the function is an integral type, (*p) on either side of the bracket. int *p () is not a variable description but a function description, stating that p is a pointer function whose return value is a pointer to an integer, *p without parentheses on either side. As a function description, it is best to write formal arguments in parentheses, which makes it easy to distinguish them from variable descriptions. For a pointer-type function definition, int *p () is just the function head part, and generally there should be a function body part.
Main () {
int i;
char *day_name (int n);
printf ("Input Day no:\n");
scanf ("%d", &i);
if (i<0) exit (1);
printf ("Day no:%2d-->%s\n", I,day_name (i));
}
char *day_name (int n) {
static char *name[]={"Illegal Day",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
Return (n<1| | N>7)? NAME[0]: name[n]);
}
This program is through the pointer function, input a 1~7 between the integer, the output corresponds to the name of the week. A description of the pointer array and a pointer to an element value that uses an array is an array of pointers. An array of pointers is a collection of ordered sets of pointers. All elements of an array of pointers must be pointer variables that have the same storage type and point to the same data type.