C Language Pointer Understanding PATR1

Source: Internet
Author: User

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

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.