C pointer-pointer operation
Before talking about pointers, let's talk about two irrelevant things and use them as a supplement to knowledge. See the following code:
# Include
Int main (void)
{
Char * s = "Hello world .";
Printf (s );
}
Will an error occur when running this code? If an error occurs, what is the error?
Analysis: a common usage of printf is printf ("Please input a data:"). I believe many people have used this usage, but I will analyze this usage, in this way, the parameter received by the printf function is the first address of a constant string, and then the string can be output, therefore, the above Code also passes the first address of the string (stored in bytes pointer s) to printf, so this code will not run any problems, even warnings are not generated during compilation. Let's look at the following code:
# Include
Int main (void)
{
Char * s = "Hello world .";
Printf (s, 1, 2, 3 );
}
Is this code correct? If not, what is the error?
Analysis: it is easy to analyze this problem. The printf function and scanf function are two typical variable-length parameter functions in C language, and printf does not know how many parameters it will receive, the only thing it knows is how many output controllers (% d) will be parsed after the input string specifiers, if the parsing fails, an error is returned. However, if the parameters are not described above, but are followed by parameters, this will be ignored, so the code compilation and running will not be a problem, or it will be the same as above, the warning will not appear during compilation.
Now let's get down to the beginning and talk about the pointer. This time we will briefly talk about the addition and subtraction operations of the pointer. Please refer to the following code:
# Include
Int main (void)
{
Int a [10];
Printf ("% p", & a [0] + 1, & a [1]);
}
Are the two values output by the program the same? What is the difference between the two?
Analysis: this problem requires a deep understanding of pointer operations to be correct. The correct answer is that the two outputs are the same, and many people may think about it when encountering this problem, after & a [0], we get the address of 0th units. This address and the address of & a [1], that is, the address of 1st units, should be a difference of sizeof (int) bytes, therefore, the two outputs should be 3 different. However, such an algorithm ignores the addition and subtraction rules of pointers. pointer or address operations often depend on the pointer or address type. For example, we often use int * p =; printf ("% d", * p ++); In this usage, p ++ can point to the next int type unit! This indicates that the value in p has been increased by 4 by the p ++ Statement, which is invisible to the operation programmer, the subsequent computation is equivalent to p = p + 1 * sizeof (int). The same is true for int-type addresses plus 1, not simply adding 1 to address values, instead, sizeof (int) is added to 1, so the two outputs are the same. This problem can be solved by defining a struct variable s, then defining an int type pointer p, using p = & s to assign values to p. In this case, the compiler will give the following information: the error or warning that char * cannot be assigned to the int * pointer. This also proves that the address-taking operation is performed on certain types of variables. The obtained data type is the data type pointer. Let's look at a more extreme example:
# Include
Int main (void)
{
Printf ("% d", (int *) 0x04-(int *) 0x00 );
}
What is the output of this program?
After the program runs, 1 is output. Of course, this is the result of running on a 32-bit machine. This example can help you understand the above analysis process.