Summary of pointer learning and pointer Learning

Source: Internet
Author: User

Summary of pointer learning and pointer Learning

1/* -------------------------------------- 2 pointer exercises (essence) 3 4 1) First, understand the first address of a variable or array, it refers to the byte address with the smallest address number in the RAM or ROM that stores data. 5 2) the type specifier before the pointer has two meanings (it determines the number of bytes that will be operated when the first-level pointer is unreferenced, and the number of addresses that will jump when performing arithmetic operations on the first-level pointer ). 6 ① determines the number of addresses to jump to when the pointer is added. 7 For example: 8 if the pointer is 9 char type, (pointer + n) the pointer jumps to n * 1 addresses. 10 int type, (pointer + n) pointer will jump to n * 2 addresses. 11 long type, (pointer + n) pointer will jump to n * 4 addresses. 12 13 ② It also determines the number of bytes actually returned when the address value is operated through the pointer, And the byte with a large address number is returned first. 14. For example, if the return value of an address is to be operated on a pointer, if the pointer is of the 16 char type, 1 byte is returned. 17 int type, returns 2 bytes. For the 18-long type, 4 bytes are returned. 19 20 the type specifiers in front of the array also have two meanings and are similar to those above. 21 For example: 22 # include "stdio. h "23 int c [] = {0x1234,0x5678}; 24 void main () 25 {26 printf (" % p % d \ n ", c, * c); // If the array is of the int type, it means two bytes are returned. 27 printf ("% p % d \ n", (c + 1 ), * (c + 1); // In fact (c + 1) and c are clamped with an address, because the array type symbol is int. If the array type is long, the value contains 3 addresses 28} 29 30, that is, the number of bytes occupied by the type, and the time when the array type symbol or pointer type symbol should be viewed. 31 3) & get the first address symbol, * call the unreferenced symbol. 32 4) array name refers to a first address. Therefore, point = a (point is a pointer and a is an array name), and no & symbol is required before. 33 variable name refers to a value, and a [1] refers to a value. These values contain one or more bytes, when you want the pointer to point to the byte address of these values, 34 must add the & symbol before the variable name and a, that is, to assign a value to the pointer (=) the thing on the right is the address. 35) array or variable data is stored in one byte, and the address of the byte is continuous. When assigning values, from left to right, 36 goes to the right, the larger the byte address number is. Therefore, for arrays of Multi-byte data types, it seems that there is a "first-and-last connection" effect, 37 because the first byte of an element corresponds to the first byte of its address, the maximum byte of the next element. 38 39 simply put, the low address stores high bytes, which is called the Large-end arrangement (commonly used single-chip microcomputer ). Note: In some cases, the low address stores low bytes, which is called the small-end arrangement (ARM ). 40 41 6) pointers can be divided into: function pointers, array pointers (multidimensional pointers), variable pointers, and struct pointers. It can also be divided into multi-level pointers and multi-dimensional pointers. Addresses can be divided into multi-level addresses and multi-dimensional addresses. 42 7) only the last element of the character array is followed by a hidden element. The value of this element is 0, and the ing character is "\ 0 ". 43) Data Pointer functions, also known as pointer functions (that is, the return value is an address ). 44 9) char (* p) [2]; // declare a two-dimensional pointer (also known as a two-dimensional array pointer) 45 when analyzing square brackets ([]) for operations on multidimensional pointers, follow the principle: first define the dimension of the pointer, then analyze the number of square brackets and the number in square brackets, and then obtain how the address jumps; 46. Then, based on the number of square brackets, we will know the number of unreferenced addresses. If the number of unreferenced addresses is less than the number of dimensions of the addresses, then the final result is an address, also, if the number of times of unreferencing is equal to the address dimension, a data value is obtained. 48 after each multi-dimensional address is unreferenced, the dimension of the address will decrease. 49 the name of a one-dimensional array is a level-1 and one-dimensional address. The name of a two-dimensional array is a level-1 and two-dimensional address. Each array name is an address. These addresses are fixed. The 50-level multi-dimensional pointer has the following features: the method of unreferencing is very special, and the address jump is very special during operation. 51 explore the Code as follows: 52 int Array [2] [3] [2] ={{ 1, 2}, {3, 4}, {5, 6 }},{ 7, 8 }, {9,10 },{ 11,12 }}; 53 54 printf ("% d \ n", Array); 55 printf ("% d \ n", Array [1]); // compared with the previous line of code, three * 2*4 = 24 addresses are redirected to 56 printf ("% d \ n", Array [1] [1]); // compared with the previous line of code, 2*4 = 8 addresses are redirected to 57 58 printf ("% d \ n ", * (Array [1] [1]); // dereference the 1-dimensional address once to obtain a data value, it is 9 59 60 printf ("% d \ n", * (Array [1]); // The 2-dimensional address is unbound twice, get a data value, which is 7 61 printf ("% d \ n", * (Array [1]); // dereference a 2-dimensional address once, obtain It is a one-dimensional address with the same value as Array [1], but Array [1] is a two-dimensional address 62 10) # include <stdio. h> 63 # include <stdlib. h> 64 int main () 65 {66 67 // here is a demonstration of illegal read/write. Although illegal read/write can be implemented, it can only exist in the same process, in addition, this situation has no practical significance 68 69 int * p; // int type pointer, operate 4 bytes 70 p = (int *) malloc (2 ); // two bytes are requested from the heap region bucket, but no Initialization is performed. The calloc Method Application initializes 71 if (p) 72 printf ("Memory Allocated: % p \ n ", p); 73 else 74 printf (" Not Enough Memory! \ N "); 75 printf (" % x \ n ", * p); // because only two bytes are applied, therefore, two bytes are illegally read, 76 * p = 0x12345678; // because only two bytes are requested, therefore, two bytes are illegally written: 77 printf ("% x \ n", * p); // because only two bytes are applied, therefore, two bytes are illegally read 78 free (p); // The two bytes applied in the heap are released, and the value in the memory may be cleared to 0, this depends on the running kernel 79 // the following 4 bytes of 80 printf ("% x \ n", * p) are read and written illegally; 81 * p = 0x87654321; 82 printf ("% x \ n", * p); 83 return 0; 84} 85 11, when pointers of different types point to different addresses and pointer types are inconsistent, an error may be reported, or it may only be a warning. 86 12) unsigned char (* q )(); // declare a function pointer in the format of 87 pointer to call the function. If you pass the parameter value, the default value is-1. The format of the pointer call function is: "(* pointer name) (parameter list)" or "pointer name (parameter list) "88 13) usage of level 1 and multi-level pointers: 89 int val = 0x1122; 90 char * p3 = & val; 91 char ** p2 = & p3; 92 93 printf ("% x \ n", p3); 94 printf ("% x \ n", p3 + 1 ); // jump to 1 Address (because p3 is a level-1 pointer and the type modifier is char) 95 96 printf ("% x \ n", * (p3 )); // operate 1 byte (because p3 is a level-1 pointer and the type modifier is char) 97 printf ("% x \ n", * (p3 + 1 )); // operate 1 byte (because p3 is a level-1 pointer and the type modifier is char) 98 99 printf ("% x \ n", (p2 )); 100 printf ("% x \ n", (p2 + 1); // jump to four addresses (Because the address length of the byte in the memory is 32 bits and the pointer p2 is a second-level pointer) 101 102 printf ("% x \ n", * (p2 )); // operate 4 bytes (because the address length of the byte in the memory is 32 bits and the pointer p2 is a second-level pointer) 103 14) to explore multi-level multi-dimensional pointers: 104 // assume that you have mastered Multi-Level One-dimensional pointers, and the use of level-1 multi-dimensional pointers is 105 106 unsigned char (** p) [3]; 107 int Array3 [10] = {0x804a0e0, 0x55667788,2, 3,4, 6, 9}; // The reason why the first element is 0x804a0e0 is that the value is improperly obtained, the following faces (* p )) during the process of unreferencing, the kernel may collapse when the program is executed. It seems that the program syntax is incorrect, that is to say, make sure that the referenced pointer is within the correct range of 108 p = Array3; 109 110 printf ("% x \ n", p ); // p is a second-level two-dimensional pointer pointing to Level 2 two-dimensional address 111 printf ("% x \ n", p + 1); // four address jumps (because the address length is 4 bytes ), because (p + 1) is a second-level two-dimensional address, that is, the output value of this statement is 4112 larger than that of the previous statement 113 printf ("% x \ n", * p ); // obtain the value of 4 bytes by unreferencing, Because p is a second-level two-dimensional pointer. * P is the first-level two-dimensional pointer 114 printf ("% x \ n", (* p + 1); // jump to 3 bytes, because * p belongs to the first-level two-dimensional pointer, that is, the output value of this statement is 3 larger than that of the previous statement. Because the jump of the first-level pointer address depends on the dimension. * p is a first-level two-dimensional pointer, the number of jumps is sizeof (unsigned char) * [3] = 3115 116 printf ("% x \ n", * (* p); // dereference a level-1 two-dimensional pointer (* p, obtain the first-level one-dimensional pointer * (* p). The print value of this row is equivalent to the first-level two-dimensional pointer * p117 118 printf ("% d \ n ", * (* p); // dereference the first-level one-dimensional pointer * (* p) and obtain the value of 1 byte. The value 0 x E0 is 224, because the type modifier of pointer p is char, and sizeof (unsigned char) = 1119 120 printf ("% d \ n", * (* p) + 1 ); // The print value of this row is 1 larger than that of the previous row, which is 225121 122. // conclusion: When multi-level multi-dimensional pointers are unreferenced, each unreferenced operation will follow the principle of downgrading first and then dimensionality reduction, when the series does not fall to 1, the dimension does not work, that is, the pointer is treated as a multi-level one-dimensional pointer. 123 // when it is already a level-1 pointer, the dimension takes effect, as a level-1 multi-dimensional pointer or level-1 one-dimensional pointer processing 124 125 126 127 ------------------------------------------*/

 

This is my summary of pointer learning. If you have any questions, please contact the author ~

Respect the labor of the author, reprinted please remember to indicate the source: http://www.cnblogs.com/weifeng727/p/5584151.html

Related Article

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.