Today, in the Head first C language, the code in the book is *lat=*lat+1; I write the *lat++; The result is that the content of the LAT pointer is not added to one. Later found that in the C language, the operator and the increment operator after + + is the same priority, *p++, the post-increment operator + + only acts on p, and does not work on *p (if the priority of the * is higher).
Later in the debugging and found a previously do not know, in C language stack storage is to the low address extension, that is, the first declared variables in memory instead of memory address is larger.
1#include <stdio.h>2 voidGo_south_east (int* lat,int*Lon) {3 //*lat=*lat-1;//Dereference The pointer parameter passed in4 //*lon=*lon+1;5 //*lat++;//in C language * dereference with + + priority is the same right-to-left combination6 //*lon--;7printf"%p\n", LAT);//lat is in the stack first declare C voice stack development direction is down so that the back of the variable in memory address is smaller8lon++;9printf"%p\n", Lon);Ten } One intMain () { A intMylat =Ten; - intmylon=Ten; - theprintf"%p%p\n",&mylat,&Mylon); -Go_south_east (&mylat,&Mylon); -printf"%p%p\n",&mylat,&Mylon); -printf"My present position is located in%i longitude%i latitude", Mylat,mylon); + - + return 0; A}
0022FEBC 0022feb80022febc0022febc0022febc 0022feb8 My present position is at 10 longitude 10 latitude --------------------------- -----0.01538return0 Press any key to continue ...
This shows that the declared variable Mylon in the memory address is less than Mylat, in the method will add a Mylon address, that is, to get mylat memory address. Here also shows the C language and I have seen the C # book the same point, for the method, transfer to a pointer to a variable, the method class can modify the contents of the pointer, the modification can be persisted to the method, and if the pointer itself is modified (pointing to the new address), the outside will not show the change, Because the transfer is only the copy of the address. In C #, you can use ref and out for reference types to point to new references, meaning when a method returns a new reference to a pointer.
C Language Pointer Understanding PATR1