C Language Learning 003: Hello pointer, language learning 003
Reasons for using pointers in C
This prevents a copy from passing data references only when calling a function.
Data sharing two pieces of code can operate on the same copy of data at the same time, instead of two independent copies
Read and Write Data Using pointers
# Include <stdio. h> int main () {int x = 5; printf ("x lives at % p \ n", & x ); // % p format operator outputs the address in hexadecimal format int * address_of_x = & x; // use the pointer variable to save the address int value of x = * address_of_x; // use the * operator to obtain the value printf ("% I \ n", value) pointed to by the pointer address; * address_of_x = 10; // change the value of the space pointed to by address_of_x address printf ("After the change, x = % I \ n", x); return 0 ;}
Captain, sail east!
# Include <stdio. h> void go_south_east (int * lat, int * lon) {// use the * operator to find the space corresponding to the lat address * lat = * lat-1; * lon = * lon + 1;} int main () {int latitude = 32; int longtitude =-64; go_south_east (& latitude, & longtitude ); // The address for transferring the variable printf ("Avast! Now at: [% I, % I] \ n ", latitude, longtitude); return 0 ;}