Understanding C pointer Reading Notes in depth-Chapter 1
Last weekend, I saw this very thin book in the library-deep understanding of the C pointer"
The content written in this book is suitable for beginners, but the content is not very difficult. I also read and take notes for each chapter. The notes are posted in the form of code.
Chapter1.h
# Ifndef _ CHAPTER_1 _ # define _ CHAPTER_1 _/* Study Notes for understanding the C pointer in depth-Chapter 1 * // * If a number is unsigned, try to use the size_t type * // * when using the size_t type, use the correct output format */void _ size_t_test (); // Add the pointer to the address pointed to by the pointer plus the size of the Data Type multiplied by the added number // The operation to subtract the original address pointed to by the pointer minus the data the value of the type multiplied by the number minus void _ pointer_add_sub_test (); /* various representations of the Pointer Modified with const */void _ const_to_pointer (); # endif
Chapter1.cpp
# Include "Chapter1.h" # include
/* If a number is unsigned, try to use the size_t type * // * when using the size_t type, use the correct output format */void _ size_t_test () {size_t test1 =-1; printf ("test1: % d \ n", test1); printf ("test1: % u \ n", test1); size_t test2 = 1; printf ("test2: % d \ n", test2); printf ("test2: % u \ n", test2);/* the output result is as follows: test1:-1test1: 4294967295test2: 1test2: 1 * // The operation to add the pointer is to add the address pointed to by the pointer plus the size of the Data Type multiplied by the number added. // The operation to subtract the pointer is the value of void _ pointer_add_sub_test () {int test1 = 1; int * p_test1 = & test1; char test2 = 'a'; char * p_test2 = & test2; printf ("p_test1-1 address is: % x \ n ", p_test1-1); printf (" p_test1 address is: % x \ n ", p_test1); printf (" p_test1 + 1 address is: % x \ n ", p_test1 + 1); printf (" \ n "); printf (" p_test2-1 address is: % x \ n ", p_test2-1 ); printf ("p_test2 address is: % x \ n", p_test2); printf ("p_test2 + 1 address is: % x \ n", p_test2 + 1 ); /* output result: p_test1-1 address is: 2dfdb8p_test1 address is: 2dfdbcp_test1 + 1 address is: 2dfdc0p_test2-1 address is: 2dfda6p_test2 address is: 2dfda7p_test2 + 1 address is: 2dfda8 */} void _ const_to_pointer () {/* pointer to a constant * // * the pointer to a constant here is only the pointer that points to a constant */int test1 = 1; const int test2 = 2;/* both operations are acceptable, but cannot be referenced and modified */const int * p_test1 = & test1; const int * p_test2 = & test2; test1 = 3;/* this is acceptable * // * p_test1 = 3; but this does not work./* points to a constant pointer of a very large amount */int test3 = 3; int * const p_test3 = & test3;/* the pointer can only point to a non-constant, because it can be used to modify the value of the variable * // p_test3 = & test1; at the same time, the pointer value cannot be modified // int * const p_test4 = & test2; the pointer can only point to a constant, because it can be modified/* constant pointer to a constant */const int * const p_test4 = & test2;/* indicates that neither the pointer nor the pointer value can be modified */}