How to understand C language pointer
This article mainly explains "how to understand C language pointer". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to understand C language pointer"!
# includeint main () {int a = 10; int b = 20; int* p = & an int** pp int** pp * define the pointer variable * p = 100 int** pp * indirect access, dereference, a = 100 p = & b p p save b address * p = 200 / / A pair of pointers take the address, the secondary pointer * pp = & a pp / solve the primary reference, the value saved by the first-level pointer p, that is, the address of a * * pp = 1000 * pp / solve the secondary reference, a = 1000 * pp = & bramp / solve the primary reference, the value saved by the primary pointer p, that is, the address of b * * pp = 2000 / / solve the secondary reference, b = 2000 int*** ppp = & pp;// third-level pointer * * ppp = & a * * ppp / solve the secondary reference, the value saved by the first-level pointer p, that is, the address of a * ppp = 5000 ppp / solve the third reference, a = 5000 * * ppp = & b * * the secondary reference, the value saved by the primary pointer p, that is, the address of b * / / unquote three references, b = 6000 printf ("% d", a); return 0;} # includeint main () {int a = 0; int b = 0; int c = 0; int* p1 = & a; int* p2 = & b; int* p3 = & c; * p1 = 100; * p2 = 200; * p3 = 300 * p3 = * p1 + * p2; * p3 = * p1 / * p2; * p3 + = 1; printf ("% d% d% d\ n", * p1, * p2, * p3); / / 100 200 return 0;}
The size of the pointer is only related to the platform
32-bit sizeof (*) = 4
64-bit sizeof (*) = 8 (bytes)
# includevoid Swap_1 (int* p1, int* p2) / / p1 int* p2 saves the addresses of an and b respectively {int* tmp = p1After / defines the pointer variable tmp, assigns the value of p1 (that is, the address of a) to tmp p1 = p2ramp / assigns the value of p2 (that is, the address of b) to p1p2 = tmp / / assign the value of tmp (that is, the address of a) to p2} / / change the value # if 0void Swap_2 (int* p1, int* p2) {int* tmp;// hanging pointer, wild pointer * tmp= * p1; * p1 = * p2; * p2 = * tmp } # endifvoid Swap (int* p1, int* p2) / / p1 int* p2 saves the addresses of an and b {int tmp; tmp = * p1 / assign the value of p1 dereference (that is, the value of a) to p1 * p1 = * p2bot / the dereference value of p2 (that is, the value of b), and assign the value of p1 dereference (that is, the value of a) * p2 = tmp / / assign the value of tmp (that is, the value of a) to the de-referenced value of p2 (that is, the value of b)} / / complete the exchange. Int main () {int a = 10; int b = 20; Swap (& a, & b); / / if the change of the main function is to affect the custom function: (1) you must pass a pointer, (2) you must dereference printf ("% dforce% d\ n", a, b); return 0;} # includeint main () {int arr [10] = {1ju Jing 2, Jing 4, Jing 5, Jing 6, Jing 7, Jing 8, Jing 9 10} For (int* p = arr; p = & arr [0]; pmure -) / / for (int* p = & arr [9]; p > & arr [- 1]; pmae -) {printf ("% d", * p);} char a = 'xtriple; char* p = & a; / / int* np = & p / / the pointer type is different return 0;} # includevoid Fun (int* p) {/ / p = NULL;// null pointer, the value of p is 0 int* p1 = pomp1 int* p2 = NULL;//p2 save NULL int tmp; tmp = * p1After / assign the dereferenced value of p1 (that is, the value of p) to tmp * p1 = * p2 / assign the value of p2 dereference (that is, NULL) to the value of p1 dereference (that is, the value of p) * p2 = tmp;// assigns the value of tmp to the value of dereference of p2} int main () {int a = 10; int* p = & a; Fun (p); / p = NULL; printf ("% d\ n", * p); return 0;} summary
Assert () (header file # include required)
Determine that the expression must be true, if it is a fake program crash, and report the error location.
User-oriented version of release: conditions need to be determined after the assert.
Void* generic pointer, generic pointer
Void* has no data type, does not participate in operation, and cannot be + + de-referenced.
Assigned general pointer = specific pointer specific pointer = universal pointer Clearcharcharp
Void* p1roomp; not allowed
(char* p
Void* p1roomp
Int* p2=p1
(p2roomp) different data types) Cchar* p
Void* p1 void * p
Char* p2roomp
C++:NULL--0
CJU NULLMI-((void*) 0)
C is defined as 0 because it does not support void* assignment.
At this point, I believe you have a deeper understanding of "how to understand the C language pointer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!