1. Pointers, references, and values
What is a pointer? What is a memory address? What is the value of a pointer? A pointer is a variable that stores the memory address of a computer. Reading data from the memory pointed to by the pointer is called the value of the pointer. Pointers can point to variable addresses of specific types, such as int,long , and double. The pointer can also be a void type,aNULL Pointer, and an uninitialized pointer.
Depending on where you appear, the operator * can be used to declare either a pointer variable or a pointer value. When used to declare a variable,* means that a pointer is declared here. Other cases Use * to indicate the value of the pointer.
& is an address operator used to refer to a memory address. By using the & operator before the variable name , we can get the memory address of the variable.
650) this.width=650; "Width=" 691 "height=" 249 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
2 * operator declares a int pointer. We then declared a int Span style= "FONT-FAMILY:CALIBRI;" >1 int The address of the variable initializes our Span style= "FONT-FAMILY:CALIBRI;" >int pointer. Next to int pointer value, initialized with the memory address of the variable. int pointer. Finally, we print the output variable value, the content is 1
The &val of line 6 is a reference. after the Val variable declares and initializes the memory, by using the address operator before the variable name & We can directly reference the memory address of the variable.
Line 8 , we once again use the * operator to value the pointer, you can directly get the data in the memory address pointed to by the pointer. Since the type of the pointer declaration is int, the value taken is the int value stored by the memory address pointed to by the pointer .
Here you can compare pointers, references, and values into envelopes, email addresses, and houses. A pointer is like an envelope, and we can fill it with a mailing address. A reference (address) is like an e-mail address, which is the actual address. The value is like the house of the address. We can erase the address on the envelope and write another address we want, but this behavior has no effect on the house.
2. Pointers and Arrays
the C-language array represents a contiguous memory space used to store multiple objects of a specific type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not of the same structure and therefore cannot be converted to each other. The array variable points to the memory address of the first element of the array.
An array variable is a constant. You cannot assign a pointer to an array variable, even if the pointer variable points to the same address or to a different array. It is also not possible to assign an array variable to another array. However, it can be confusing to assign an array variable to the pointer. When assigning an array variable to a pointer, it is actually assigning the address to the first element of the array to the pointer.
650) this.width=650; "Width=" 691 "height=" 353 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
3, the pointer and the structure body
Just like an array, a pointer to a struct stores the memory address of the first element of the struct body. Like an array pointer, a struct's pointer must be declared to be consistent with the struct type, or declared as a void type.
650) this.width=650; "Width=" 691 "height=" 431 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
The1to6Line declares a Personstruct, a variable points to a Personstructure and Point Personthe pointer to the struct body. Section8Behavior Agemembers have assigned aintvalues. Section9toTenYes, we've declared aCharpointer and assign a value to aChararray and assign a value to the struct bodynamemembers. Section OneOkay, let's put a Personstruct references are assigned to struct variables.
The -Yes, we have printed the structure of the struct instance. Ageand thename. Here are two different symbols to note, '.' and ' -'. struct instances can be used by using the '.' Symbolic access Agevariable. For pointers to struct instances, we can pass the ' -' Symbolic accessnamevariable. You can also pass the same(*ptr). Nameto accessnamevariable.
Lesson three Homework