January 15, 2017 pointer learning finishing

Source: Internet
Author: User

About the concept of pointers:

A pointer is a special variable that is stored in a value that is interpreted as an address in memory.

First of all: we need to be clear about our goals

About pointer learning and use we need to figure out four things about pointers: the type of pointer, the type that the pointer points to, the value of the pointer (or the memory area that the pointer points to), and the memory area that the pointer itself occupies (the pointer is also a special variable, it must also occupy memory). Next, let's study separately.

Let's start by stating some examples of pointers:

1 int *ptr;   2 Char *ptr;   3 int * *ptr  ; 4 int (*PTR) [3];   5 int * (*ptr) [4

The first: the type of the pointer

from a grammatical point of view, you declare a pointer to a statement (such as:int *ptr; the pointer name is removed as the type of the pointer itself. (There are many different types of pointers)

int *ptr;  // pointer type is int * Char *ptr;  // pointer type is char * int **ptr;  // pointer type is int * * int (*PTR) [3]; //   the pointer type is int (*) [3]int * (*ptr) [4]; // pointer type is int * (*) [4]

You alright! This part should be well understood.

The Second: The type that the pointer points to

When you use a pointer to access the memory area pointed to by the pointer, the type that the pointer points to determines what the compiler will look for in that memory area.
Syntactically, you only have to remove the pointer name and the pointer to the left of the name in the pointer declaration, and all that remains is the type that the pointer points to. For example:

int // the type that the pointer points to  is int Char // The type that the pointer points to is  Char int // The type that the pointer points to is int  * int (*PTR) [3// The type pointed to by the pointer is int () [3  ]int * (*ptr) [4/// The type that the pointer points to is int * () [4  ]

In the arithmetic operation of a pointer, the type that the pointer points to has a great effect.
The type of the pointer (that is, the type of the pointer itself) and the type that the pointer points to are completely different concepts. As you become more familiar with C, you will find that the concept of "type", which is mixed with pointers, is divided into "types of pointers" and "types pointed to by pointers", which is one of the key points of mastery of pointers. I read a lot of books, found that some poorly written books, the pointer to the two concepts stirred together, so look at the contradiction between the book, the more confused (want to learn the pointer or a good look at my blog bar-_-).

The third: The value of the pointer

The value of the pointer is the value stored by the pointer itself, which is treated as an address by the compiler, not a generic value. In a 32-bit program, the value of all types of pointers is a 32-bit integer because the memory address in the 32-bit program is all 32 bits long.
The memory area that the pointer points to is the memory address from which the value of the pointer is represented, and the length of the memory area of sizeof (the type the pointer is pointing to). Later, we say that the value of a pointer is XX, which is equivalent to saying that the pointer to an XX-led address of a piece of memory area; we say that a pointer points to a region of memory, which is equivalent to saying that the value of the pointer is the first address of the memory area.
The memory area that the pointer points to and the type that the pointer points to are two completely different concepts. In example one, the pointer points to a type that already exists, but because the pointer is not initialized, the memory area it points to is nonexistent, or meaningless.
Later, every time you encounter a pointer, you should ask: What is the type of this pointer? What type is the pointer pointing to? Where does the pointer point? (especially in the beginner stage, just start learning to take a little time doesn't matter, otherwise later ...) )

The fourth: the memory area occupied by the pointer itself

How much memory does the pointer itself occupy? You just need to use the sizeof (pointer type) function to find out. In a 32-bit machine, the pointer itself occupies a length of 4 bytes.

The 64-bit machine should be 8 bytes (specifically forgotten, useful for the 64-bit self-test, re me.) )

The concept of the memory occupied by the pointer itself is useful in determining whether a pointer expression is an lvalue (which is later omitted here).

OK, here is the four-pointer, it is not a simple feeling!

Arithmetic operations for pointers:

We already know that the pointer is actually a special variable, so it is possible to perform the operation. The operation of pointers is not the same as the usual arithmetic operations.

eg

Char a[];    int *ptr=A;  ...  ...   PTR+ +;  

We defined the char array A, the pointer ptr initialized to the shaping variable A, where the pointer points to the memory of the char array a starting point, that is, pointing to a[0], and then ptr++, however it is not +1 so simple. We assume that the type that the PTR pointer is pointing to is T, then ptr++ is equivalent to ptr+=sizeof (t). In the example above, because Char occupies a byte, ptr is exactly +1, at which point he points to a[1]. If T is of type int, ptr++==ptr+sizeof (int) {sizeof (int) ==4}, it also points to a[1] by pointing to a[0];

Char a[];   int *ptr = A;  ...  ...   5;  

In This example, PTR is added with 5, and the compiler handles this by adding the value of the pointer ptr to 5 by sizeof (int), which adds 5 times 4=20 to the 32-bit program. Because the unit of the address is byte, the current PTR points to an address that is 20 bytes to the high address direction than the address pointed to by PTR after 5. In this example, the PTR before 5 points to the four bytes starting at Unit No. 0 of array A, plus 5, PTR has pointed out to the legal range of array a (at this point ptr is pointing to something that no one knows, perhaps another program's memory, perhaps an empty memory, and possibly the system does not allow access). Although this situation may be problematic in application, it is syntactically possible. This also shows the flexibility of the pointer.

if PTR is subtracted by 5 in the example above, then the process is much the same, except that the value of PTR is subtracted by 5 by sizeof (int), and the new PTR points to an address that moves 20 bytes to the lower address direction than the address pointed to by the original PTR.

summing up, after a pointer ptrold plus an integer n, the result is a new pointer ptrnew,ptrnew of the same type as the Ptrold, and the same type that ptrnew points to and the type that ptrold points to. The value of ptrnew will increase by N by sizeof (the type that the Ptrold points to) by more than the value of Ptrold. That is, the memory area pointed to by Ptrnew will move n by sizeof (the type that the Ptrold points to) bytes than the memory area pointed to by Ptrold to the high address direction. After a pointer ptrold minus an integer n, the result is a new pointer ptrnew,ptrnew of the same type as the Ptrold, and the same type that ptrnew points to and the type that ptrold points to. The value of ptrnew will be less than the value of Ptrold by sizeof (the type that the Ptrold points to) bytes, that is, the memory area that the ptrnew points to will move n by sizeof (the type that the Ptrold points to) than the memory area pointed to by Ptrold A byte.

Operators & and *

Here & is the fetch address operator, * is ... The book is called the "indirect operator." The result of the &a operation is a pointer, the type of the pointer is a type of a, a pointer to the type of a, the pointer to the address, that is the address of a. The result of *p's operation is very multifarious. In short *p The result is what P points to, this thing has these characteristics: its type is the type of P, which occupies the address that P points to.

eg

1 intA= A; 2 intb; 3 int*p; 4 int**ptr; 5p=&a;//The result of the &a is a pointer to the type int*, the type is int, and the address to a is the address of a. 6*p= -;//*p The result, where its type is int, and it occupies the address that P points to, it is clear that *p is the variable A, that is, a is assigned a value of 24. 7ptr=&p;//The result of the &p is a pointer, the type of the pointer is the type of P plus a *, here is the int**. The type that the pointer points to is the type of P (int*). The pointer points to the address that is the pointer P's own address (loop, think about the ANG). 8*ptr=&b;//*ptr is a pointer, and the  result of &b is also a pointer , and the two pointers are of the same type as the one they are pointing to, so the amp;b to assign to *ptr is no problem. 9**ptr= the;//The result of the *ptr is what PTR points to, and here is a pointer to do another * operation on the pointer, and the result is a variable of type int. 

I'd better write a few more small programs to practice, practice a few times to understand.

pointer expression:

The final result of an expression if it is a pointer, then the expression is called the pointer expression. Here are some examples of pointer expressions:

1 intb; 2 intarray[Ten]; 3 int*PA; 4pa=&a;//&a is a pointer expression. 5 int**ptr=&pa;//&pa is also a pointer expression. 6*ptr=&b;//both *ptr and &b are pointer expressions. 7Pa=Array; 8pa++;//This is also an expression of pointers. 

 1  char  *arr[ 20    2  char  **parr=arr;// 3  Span style= "color: #0000ff;"  >char  *STR;  4  Str=*parr; // *parr is a pointer expression  5  str=* (parr+1 ); // * (parr+1) is a pointer expression  6  str=* (parr+2 ); // * (parr+2) is a pointer expression  

Because the result of a pointer expression is a pointer, the pointer expression also has four features that the pointer has: the type of pointer, the type that the pointer points to, the memory area that the pointer is pointing to, and the memory that the pointer itself occupies.

Well, when the result pointer of a pointer expression has explicitly had the memory occupied by the pointer itself, the pointer expression is an lvalue, otherwise it is not an lvalue. In example VII, &A is not an lvalue because it does not yet occupy a definite amount of memory. *ptr is an lvalue, because *ptr this pointer has occupied the memory, in fact *ptr is the pointer PA, since PA has in memory has its own position, then *ptr of course also has its own position.

The relationship between arrays and pointers:

eg

1 intarray[Ten]={0,1,2,3,4,5,6,7,8,9},value; 2 ...  3 ...  4value=array[0];//can also be written as: Value=*array; 5value=array[3];//can also be written as: value=* (array+3); 6value=array[4];//can also be written as: value=* (array+4); 

In the above example, the array-name array typically represents the array itself, and the type is int [10], but if you think of array as a pointer, it points to the No. 0 element of the array, the type is int *, and the type to which it refers is the type of the array cell, or int. So it's not surprising that *array equals 0. Similarly, array+3 is a pointer to the 3rd cell of the array, so * (array+3) equals 3. Other and so forth.

The relationship of pointers and struct types:

Can be used to declare a pointer to a struct type object.

eg

1 structMyStruct2 {  3 intA; 4 intb; 5 intC; 6 }  7 8MyStruct ss={ -, -, +};//The struct object SS is declared, and the three members of the SS are initialized to 20,30 and 40. 9MyStruct *ptr=&ss;//a pointer to the struct object SS is declared. It is of the typeTenmystruct*The type that it points to is mystruct.  One int*pstr= (int*) &ss;//a pointer to the struct object SS is declared. But its type and the type it points to and PTR are different. 

How can I access the three member variables of SS through pointer ptr?

Ptr->A;  PTR-b;  PTR->c;  

The relationship between pointers and functions:

A pointer can be declared as a pointer to a function.

int fun1 (char*,int);   int (*PFUN1) (Char*,int);  Pfun1=fun1;  ....  ....   int a= (*pfun1) ("abcdefg",7); // functions are called through function pointers.  

You can use a pointer as a parameter to a function. In a function call statement, you can use a pointer expression as an argument.

The pointer to the end of this declaration

January 15, 2017 pointer learning finishing

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.