Pointers to small secrets in C Language (2)

Source: Internet
Author: User

Every blog now requires a summary. However, if I start talking about the previous article directly, the first time I read my blog, I don't know what I'm talking about, so I should move the first section of the previous blog as a summary.

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.

I would like to emphasize that, in the future, the first section of my blog will be used as a summary. If you have already read the abstract in the previous blog, you can skip the duplicate summary and directly enter the body.

Next, let's look at the pointer of the array and the pointer variable pointing to the array. The pointer to the array is the starting address of the array, and the pointer to the array element is the address of the array element. You can use either of the following methods to reference an array element:

1. subscript method, such as a [8].

2. pointer method.

The advantage of using the pointer method is that the target program occupies less memory and runs fast, thus improving its quality. Why do pointers have the following advantages? I think it is necessary to give some explanations here, because pointers occupy 4 bytes on 32-bit machines, if the function transfers an object that occupies a large amount of memory, for example, int A [2000], it is obvious that it is easy to transmit with pointer reference, saving memory and saving time for copying objects; if we use the subscript method to reference an array, we have to retrieve the starting address of the array and convert it to direct addressing through base + offset, which is more efficient than the pointer.

From the above, we can see that the pointer is very powerful and can accomplish many things. The essence of C lies in the pointer, so that C can be close to the efficiency of ASM. Therefore, when writing a program, it is necessary to make full use of the advantages of pointers to compile efficient C language code.

Let's take a look at the Code:

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

Int main ()
{
Int A [8];
Int * P;
// **************************** Print array a ***** by subscript ***** *********************//
Printf ("\ n **************************** print array A by subscript ** * *********************** \ n ");
For (INT I = 0; I <8; I ++)
{
A [I] = I;
Printf ("A [% d] = % d \ t", I, a [I]);
}
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

// **************************** Print ******* using pointer Variables ******* *******************//
Printf ("\ n *************************** print with pointer variables **** * ********************* \ n ");
P =;
For (Int J = 0; j <8; j ++)
{
Printf ("P % d = % d \ t", J, * P ++ );
}
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//
Printf ("\ n ");

// ************************ Print ********* using array name pointer Calculation ******** *****************//
Printf ("\ n ************************ print ****** using array name pointer operation ***** * ******************* \ n ");
For (int K = 0; k <8; k ++)
{
Printf ("A [% d] = % d \ t", K, * (a + k ));
}
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//
 
// **************************** Print the value address of two-dimensional array B *** *****************//
Printf ("\ n **************************** print the value and address of two-dimensional array B * ****************** \ n ");
Int B [4] [4];
For (INT n = 0; n <4; n ++)
{
For (INT m = 0; m <4; m ++)
{
B [N] [m] = N * m;
Printf ("% d \ t", B [N] [m]);
Printf ("% d \ t", & B [N] [m]);
}
Printf ("\ n ");
}
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

Int * PP = & B [0] [0];
Int ** PPP = & PP;

// ******************** Address of two-dimensional array B, and PP and * PPP value **** ************//
Printf ("\ n ********************* address of two-dimensional array B, and PP and * PPP value * * ************** \ n ");
Printf ("\ n & B [0] [0] = % d \ TPP = % d \ t * PPP = % d \ n ", & B [0] [0], PP, * PPP );
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

// **************** Two-dimensional array B [0] [0], and * PP and ** PPP values ** ****************//
Printf ("\ n ***************** two-dimensional array B [0] [0], and * PP and ** PPP value ******************* \ n ");
Printf ("\ NB [0] [0] = % d \ t * PP = % d \ t ** PPP = % d \ n ", B [0] [0], * PP, ** PPP );
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

// **************************** Print ******* using pointer Variables ******* *******************//
Printf ("\ n *************************** print with pointer variables **** * ********************* \ n ");
For (Pp = & B [0] [0]; pp <(& B [0] [0] + 16); pp ++)
Printf ("% d \ t", * PP );
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

Printf ("\ n & PP = % d \ t & PPP = % d \ n", & PP, & PPP );

Printf ("\ nppp = % d \ t * PPP = % d \ n", PPP, * PPP );

Printf ("% d \ t", * (* ppp-1 ));

Return 0;
}

When writing the code above, I added a lot of comments and printed instructions to make the Code look not very beautiful, but it will not affect our reading of the code at all, next let's take a look at the running results and then align them for analysis.

The image above may be a little too large. Because the image looks better, I still copy the print result while uploading the image. If the image cannot be opened due to network reasons, see the following running result:
* ************************** Print array a ******* by subscript ******* *******************
A [0] = 0 A [1] = 1 A [2] = 2 A [3] = 3 A [4] = 4 A [5] = 5 A [6] = 6 A [7] = 7
* ************************** End ********* *********************

* ************************** Print ********* using pointer Variables ********* *****************
P0 = 0 p1 = 1 P2 = 2 P3 = 3 P4 = 4 P5 = 5 P6 = 6 P7 = 7
* ************************** End ********* *********************

* ********************** Print *********** with array name pointer operation ********** ***************
A [0] = 0 A [1] = 1 A [2] = 2 A [3] = 3 A [4] = 4 A [5] = 5 A [6] = 6 A [7] = 7
* ************************** End ********* *********************

* ************************ Print the value and address of two-dimensional array B ***** **************
0 1244944 0 1244948 0 1244952 0 1244956
0 1244960 1 1244964 2 1244968 3 1244972
0 1244976 2 1244980 4 1244984 6 1244988
0 1244992 3 1244996 6 1245000 9 1245004

* ************************** End ********* *********************

******************* Address of two-dimensional array B, and PP and * PPP value ****** **********

& B [0] [0] = 1244944 pp = 1244944 * PPP = 1244944

* ************************** End ********* *********************

* ************** Two-dimensional array B [0] [0], and * PP and ** PPP values **** **************

B [0] [0] = 0 * PP = 0 ** PPP = 0

* ************************** End ********* *********************

* ************************** Print ********* using pointer Variables ********* *****************
0 0 0 0 1 2 3 0 2
4 6 0 3 6 9
* ************************** End ********* *********************

& PP = 1244932 & PPP = 1244928

PPP = 1244932 * PPP = 1245008
9 press any key to continue

First, let's take a look at the three methods used to print the one-dimensional array a. Each element in array a is successfully printed. The next class is a process of printing two-dimensional array B, when printing each element in array B, we also print the corresponding address. careful readers may send the rule between addresses, because we declare the int type, therefore, each element occupies 4 bytes, and the address difference between adjacent elements is 4.

Next, we use a pointer PP and a pointer PPP pointing to the pointer. Pay special attention to its usage when using the pointer PPP. Through the print statement, we print out & B [0] [0], PP, * PPP, all of which have the same results, are the addresses of the Two-dimensional array B [0] [0], so the following printed B [0] [0], * PP, ** PPP is the value of B [0] [0]. Next we use the pointer method to successfully print the two-dimensional array B.

Next, we printed the addresses of pointer PP and dual-pointer PPP, and also the values of PPP and * PPP, note the relationship between * PPP value and the address of the last printed array element. Why does this result appear, because we previously used an int ** PPP = & PP;, so that * PPP and PP point to the same bucket, the address is & PP = 1244932, so when the PP value is changed, * the value of PPP is also changing. So careful readers may find in the last print statement printf ("% d \ t", * (* ppp-1);, we use * (* ppp-1) to successfully print the last element of the Two-dimensional array B.

For how to use the two-dimensional pointer class to print the array, see the following code:

# Include <stdio. h>

Int main ()
{
// **************************** Print the value address of two-dimensional array B *** *****************//
Printf ("\ n **************************** print the value and address of two-dimensional array B * ****************** \ n ");
Int B [4] [4];
For (INT n = 0; n <4; n ++)
{
For (INT m = 0; m <4; m ++)
{
B [N] [m] = N * m;
Printf ("% d \ t", B [N] [m]);
Printf ("% d \ t", & B [N] [m]);
}
Printf ("\ n ");
}
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

Int * PP = & B [0] [0];
Int ** PPP = & PP;

// ***************************** Print array B using 2D pointers *** ****************//
Printf ("\ n **************************** print array B using a two-dimensional pointer * * ***************** \ n ");
For (* PPP; * PPP <(& B [0] [0] + 16); (* PPP) ++)
Printf ("% d \ t", ** PPP );
Printf ("\ n *************************** end **** * ************************* \ n ");
// *************************** End ******* ***********************//

Return 0;
}

The running result is as follows:

Pay attention to the red mark in the Code. Many people use the two-dimensional pointer ** which is the most prone to errors when printing PPP. Many users use the following method:

For (* PPP; * PPP <(& B [0] [0] + 16); * PPP ++)

It seems that there is no problem, and it seems that the correct results can be obtained. However, after careful analysis, we will find that the problem is located, because ++ has a higher priority *, so the first step is the PPP ++ operation, and then the * PPP operation. In this case, we will see the problem of the wild pointer. Therefore, when printf ("% d \ t", ** PPP) is called, a memory error occurs. Therefore, we need to add a bracket (* PPP) ++ so that * PPP is the address of B [0] [0, then, the elements of two-dimensional array B can be successfully printed by using ++ and printf ("% d \ t", ** PPP.

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.