Pointers to small secrets in C Language (1)

Source: Internet
Author: User

Anyone who knows C language knows that C language is powerful and free, most of which are reflected in its flexible pointer usage. Therefore, pointer is the soul of the C language, and it cannot be said at all. So I added a title (1) to show the importance of pointers. I try my best to explain my understanding of pointers. So in the course of explanation, I try my best to use the code and text description, and analyze the code to deepen our understanding of the pointer. All the code I provide is complete, therefore, you can directly copy the file to run it during the reading process. I hope the following explanation will help you.

First, let's take a look at the general form of defining a pointer:

Base type * pointer variable name

After reading the definition form of the pointer above, we may have some questions. For example, why do we need to specify the base type? Because we all know that the number of bytes in the memory of integer and numeric types is different. When we move the pointer and perform pointer operations, if the pointer points to an integer variable, the pointer moves four bytes at a position, but if the pointer points to a variable of the bytes type, the pointer moves one byte, therefore, we must specify the base type to which the pointer variable points.

Let's take a look at the following code for a non-boring explanation. (Note: All the code in this blog is compiled and run using vc6, so some rules may be slightly different from those in C)

# Include <stdio. h>

Int main ()
{
Int A, B;
Int * pointer_1, * pointer_2;
A = 100;
B = 200;
Pointer_1 = &;
Pointer_2 = & B;
Printf ("-------------------- before transformation ------------------- \ n ");
Printf ("A = % d \ TB = % d \ n", a, B );
Printf ("* pointer_1 = % d \ t * pointer_2 = % d \ n", * pointer_1, * pointer_2 );
* Pointer_1 = 300;
Int c = 500;
Pointer_2 = & C;
Printf ("-------------------- after transformation ------------------- \ n ");
Printf ("A = % d \ t * pointer_1 = % d \ n", A, * pointer_1 );
Printf ("c = % d \ TB = % d \ t * pointer_2 = % d \ n", C, B, * pointer_2 );
}

The running result is as follows:

Here we define two integer pointers: int * pointer_1, * pointer_2; which point to the variables A and B, respectively, it is worth noting that * pointer_1 and A, * pointer_2 and B share the same bucket. When we change * pointer_1 = 300; in the code of the next class, the output shows that the value of a also changes. However, when we declare an int c = 500;, the value of B remains unchanged when pointer_2 = & C; is used, which only changes * pointer_2, because I only changed * pointer_2 pointing to the C bucket. If interested, you can verify it yourself. If we modify the value of A, * the value of pointer_1 will change along with each other, because they point to the same bucket.

Next, let's take a look at how to use pointers in function parameters.

# Include <stdio. h>

Swap (INT P1, int P2)
{
Int temp;
Temp = p1;
P1 = P2;
P2 = temp;
}

Int main ()
{
Int A, B;
Int * pointer_1, * pointer_2;
Int C, D;
C =;
D = B;
Pointer_1 = &;
Pointer_2 = & B;
A = 20;
B = 30;
Swap (A, B );
Printf ("A = % d \ TB = % d \ n", a, B );
Printf ("A = % d \ TB = % d \ n", * pointer_1, * pointer_2 );
Printf ("c = % d \ TD = % d \ n", C, D );
}

Preliminary analysis of the above Code seems to require a function call to achieve a A, B exchange, and through C = A;, D = B; to assign initial values to C and D. Let's take a look at the following running results:

The result is different from what we think. The reason why a and B do not implement the exchange is that we use data transfer instead of data transfer, therefore, the processing during the call process is to copy the values of A and B to the P1 and P2, therefore, the exchange operation is performed in space P1 and P2, so the values of A and B are not affected. Why is the initial values of C and D not the same as those of A and B, because when we assign the initial values to C and D during the initialization process, the initial values of A and B are not given, therefore, the initial values of A and B are given by the system during the compilation process, because the space of C and D we applied for is irrelevant to A and B, therefore, when values of A and B are assigned, the initial values of C and D will not change.

Next code:

# Include <stdio. h>

Swap (int * P1, int * P2)
{
Int * temp;

Temp = p1;
P1 = P2;
P2 = temp;
}

Int main ()
{
Int A, B;
Int * pointer_1, * pointer_2;
Int C, D;
C =;
D = B;
Pointer_1 = &;
Pointer_2 = & B;
A = 20;
B = 30;
Printf *************** * ** \ n ");
Printf ("A = % d \ TB = % d \ n", a, B );
Swap (pointer_1, pointer_2 );
Printf *************** * ** \ n ");
Printf ("A = % d \ TB = % d \ n", a, B );
Printf ("* pointer_1 = % d \ t * pointer_2 = % d \ n", * pointer_1, * pointer_2 );
Printf ("c = % d \ TD = % d \ n", C, D );

Return 0;
}

Let's take a look at the above Code, which seems to meet the requirements of our previous address transfer. Let's take a look at the experiment results first.

The results seem to be unexpected. Why is it still impossible to use the data transfer function? If we add printf ("* P1 = % d \ t * P2 = % d \ n", * P1, * P2); to the call function, the following result is obtained:

From the result, it seems that we have implemented the exchange, but why can't we return it? Note that the exchange statement in the function only changes the values of the local pointer variables P1 and P2, so the values of A and B are not changed, therefore, using printf ("* P1 = % d \ t * P2 = % d \ n", * P1, * P2) makes us see the illusion of a and B exchange, it only changes the values of local variables P1 and P2.

Next code:

# Include <stdio. h>
# Include <stdlib. h>

Swap (int * P1, int * P2)
{
Int * temp;
Temp = (int *) malloc (sizeof (INT ));
* Temp = * P1;
* P1 = * P2;
* P2 = * temp;
Free (temp );
}

Int main ()
{
Int A, B;
Int * pointer_1, * pointer_2;
Int C, D;
C =;
D = B;
Pointer_1 = &;
Pointer_2 = & B;
A = 20;
B = 30;
Printf *************** * ** \ n ");
Printf ("A = % d \ TB = % d \ n", a, B );
Swap (pointer_1, pointer_2 );
Printf *************** * ** \ n ");
Printf ("A = % d \ TB = % d \ n", a, B );
Printf ("* pointer_1 = % d \ t * pointer_2 = % d \ n", * pointer_1, * pointer_2 );
Printf ("c = % d \ TD = % d \ n", C, D );

Return 0;
}

Let's see the result:

Finally, an expected result is displayed. From the above analysis, the reader also knows the reason. Here, the operation is the address pointed to by P1 and P2, to truly exchange the values of A and B buckets. Careful readers may see the Code marked with red in the code, which can be replaced by an int temp;. The reason why we should use int * temp here; it is nothing more than remembering some special usage of pointers. If we do not have this sentence temp = (int *) malloc (sizeof (INT ));, the above Code does not produce any errors during the compilation process, but errors may occur during the running process. Therefore, when we use pointers, pay special attention to the appearance of wild pointers to avoid some inexplicable errors.

 

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.