On the C language pointer principle

On the C language pointer
learning targets, we must understand what is a pointer. The definitions given in most textbooks as: "address pointer to a variable called the variable" .
Thus, first of all let's talk about computer memory addresses. We all know that running a C language program needs to be loaded from disk into memory, perform an operation. The so-called abstract operation may be appreciated by the instruction address is read a computer processor, the process data. In memory, each byte (8 bits) corresponding to an address, consecutive addresses corresponding to consecutive memory space, the value stored in the storage space can be obtained by the processor address. To facilitate understanding, we include the principles of the value of the variable accessed by a computer: when we define a local variable, for example, short a = 7, the system will randomly assign some number of contiguous memory bytes on the stack 2, each byte there will be an address. Assumed that the system address is assigned to a variable 0x00-0x01, then these two bytes will be stored to represent 00000000 and 00000111 7, when we visit a variable, we get a first address 0x00 variables are then defined according to base type (short type) fetches the corresponding data bytes

We all know that does not support 32-bit processor, 4GB + memory to run, because the maximum address 32-bit processor can access 8-bit hexadecimal representation, namely the maximum access address 0xffffffff, about 4G, more than 4G of memory, processor access less.
As a result, we understand the relevant knowledge of computer addresses, in C language, there are a number of first address memory space corresponding to the pointer is this number, you need to pay attention to, pointers are represented in hexadecimal constants. Because memory is randomly assigned by the system in the stack, so we can not directly operate on the pointer, thereby introducing a variable to hold the special address, this variable is called a pointer variable. We all know, C language for data access mainly through two forms: 1. Direct variable name to access (direct access) by 2 to get the value of (address) pointer variable, and then access the address. ( In fact, the two are by nature address access, the so-called variable name does not exist in memory, the compiler is to see, according to the variable name when accessing the memory space, the compiler control symbol table (for language translator a data structure, in the symbol table, the program source code are each identifier and its use of information or statements bound, such as its data type, scope and memory addresses) the fetch address, memory access )

Our in-depth understanding of C language pointer from the following four aspects of the
reference variable (indirect access) through a pointer
pointer variable is a variable to store the address, let's review what is variable. Variables: Represents a name, a memory cell having a particular attribute. It is used to store data. From the definition of the information we can get two ○ 1 variable is not a specific value, but a value of the variable storage unit stored ○ 2 may vary

Pointer variable is defined: for example, the variable name yl Type * int . A meaning defined as a pointer variable pointing to a first address int-type data, the address pointer variable is
a pointer variable assignment: the value of the address pointer variable due to the assignment of p = & a, a variable address assigned to p. We pointer variable value assigned to the address, the ultimate aim is to operate through the address value stored in the address memory variables. In the C language, we usually
represent a specific value of the pointer variable pointer to the variable name. The point = 1 (point to point variable is assigned to 1). Of course, we want the pointer variable group defined type when variable names distinguish, in the definition of the variable to point to only that group of type pointer
total is well known, can be directly manipulated variable in addition, can also be passed as a function parameter, pointer variable as a variable naturally have this property, we can help to pass over the other functions of the variables in the variable address to operate
by reference to an array of pointers
to see how pointers array of references, we start with the most simple one-dimensional array start. By reference array to array elements is nothing less than two ways, one standard method is to array A pointer method. Here, we explore how the main reference one-dimensional array pointer method. The first point we must be clear, whether we use the pointer reference variables, arrays, or functions. We are operating their first address. When referring to an array of variables, we obtain a variable first address, fetches the corresponding byte of data from the memory according to the type of declaration. One-dimensional array is a group of the same type, an ordered set of data, the first address array of delegates, so we assign a value to the name of the array pointer variable, we can refer to the array through a pointer variable. For example: in int a [] = {1,3,5,7,9}, we how to access the array element 5. First, we define a pointer pointing to the array variable int P = A. Then p + 2 that is a [2] of the address (the array is [0] from a),(P + 2) is the value of a [2] 5. Note here 2, C language compiler will automatically be converted to an array of the type defined by 2 again, assuming the above-described first address 0x00 array, because the definition is int type 4 bytes, the computer finds 0x00 + 2 * 4 = 0x08 in, and remove the 4 bytes shall 5

Next, we discuss two-dimensional array, also known as two-dimensional array matrix, in order to visualize the understanding of the two-dimensional array, we will write a two-dimensional array of rows and columns (actually a linear structure), we talked about earlier references to an array all through the first address. So how do we represent the row and column addresses by the first operation. 3, for example, we define a two-dimensional line array of four int int A [3] [4], line 3 represents 3, 4 represents 4 (first row plus 1 0, the first row. The first row 0 elements 0 plus 1, 0 represents the first element row), how we represent the second line of the first address it? Speaking in front of an array name to represent the first address, address line 2, we can be represented by a + 2. How do we address the second row represents the second element of it? It is not used (a + 2) +2 to represent? Obviously not, this will indicate to some extent misunderstood, so we add a pointer to the front line , they become a pointer to the column, so it is easy to represent the second line of the second element address (a + 2) +2, we can further value representing the second line of the second element ( (a + 2) +2). Array is a contiguous memory space in memory, we can understand the second row than the first row the last element of at least one element of group type byte, the value of the second row of the second element may also be expressed as a * (a +10). The above pointer is accessed. How do we address the ranks to represent it with a subscript? Actually very simple, two-dimensional array is often referred to as "array of arrays", such as above a [3] [4], we can address it as a led to an array name a a [3] represents an array of rows and respectively, a [0], a [1 ], a [2], a [3] is int array name [4] column array, so that the above-described array row 2 second element address can be represented as a [2] +2, which value is expressed as a [2] [2].

通过指针引用字符串
引用字符串有两种方式:1.利用字符数组 2.用字符指针变量
例如1.char string[14]=”I LOVE CHINA!”;
2.char * string=”I LOVE CHINA!”;
第一种方式我们可以这样理解,系统创建了一个14个字节的内存空间,将字符串逐个存放,最后一位存放‘\0’,数组名代表字符数组首地址
第二种方式中,由于C语言对字符串常量是按照字符数组来处理,但存入后,该字符数组没有名字。因此不能通过数组名引用。只能通过指针变量来引用。上述第二条语句实际是将字符串首地址赋给string

通过指针引用函数
什么是函数指针?我们编译一个函数,编译系统为函数代码分配一段存储空间,这段空间起始地址称为函数指针
定义:>int (*p)(int int)
引用:将拥有相同类型参数的函数名(起始地址)赋给p,p就可以调用该函数

最后再总结一下const关键字在指针中的规则:

Const int p=&i:const在前,表示不能通过指针去修改变变量
Int
const p=&i:const在后,表示p这个指针不能再保存别的地址值

Guess you like

Origin www.cnblogs.com/miaowulj/p/12236154.html