C + + Pointers detailed introduction and summary of _c language

Source: Internet
Author: User
Tags arithmetic numeric value

The concept of pointers:

A pointer is a special variable that is stored in a value that is interpreted as an address in memory. To figure out a pointer needs to figure out four aspects of the pointer: the type of the pointer, the type the pointer is pointing to, the value of the pointer, the memory area that the pointer points to, and the memory area occupied by the pointer itself. Let's explain separately.

Let's declare a few pointers to do the example:

Example one:

int *ptr; 
char *ptr; 
int **ptr; 
int (*PTR) [3]; 
int * (*PTR) [4]; 

Type of pointer

From a grammatical point of view, you simply remove the pointer name from the pointer declaration statement, and the rest is the type of pointer. This is the type that the pointer itself has. Let's look at the types of each pointer in example one:

int *ptr; The type of the pointer is int * 
char *ptr;//pointer type is char * 
int **ptr;//pointer type is int * * int 
(*PTR) [3];//pointer type is int (*) [3] 
I NT * (*PTR) [4]; The type of the pointer is int * (*) [4] 

What do you think? How easy is it to find the type of pointer?

The type that the pointer points to

When you access the memory area that the pointer points to by using the pointer, the type that the pointer points to determines what the compiler treats the contents of that memory area as.

Syntactically, you simply remove the pointer's name from the pointer declaration statement and the pointer to the left of the name, and the rest is the type that the pointer points to. For example:

int *ptr; The type that the pointer points to is an int char *ptr;//the type to which the pointer refers is a 
char 
int **ptr;//the type to which the pointer refers is int * 
int (*PTR) [3];//the type to which the pointer refers is int () [ 3] 
int * (*PTR) [4];//the type to which the pointer refers is int * () [4] 

In the arithmetic operation of a pointer, the type that the pointer points to has a significant effect.

The type of the pointer (that is, the type of the pointer itself) and the type to which the pointer points are two concepts. As you become more and more familiar with C, you will find that the concept of "type", which is mixed with pointers, is divided into "type of pointer" and "type of pointer", which is one of the key points in mastering pointers. I read a lot of books, found that some poorly written books, the two concepts of the pointer stirred together, so look at the book to contradictions, the more the more confused.

The value of the pointer

The value of a pointer is a numeric value stored by the pointer itself, which is treated as an address by the compiler, not as 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 area that begins with the memory address represented by the value of the pointer, and the length of the sizeof (the type to which the pointer points). Later, we say that the value of a pointer is XX, it is equivalent to say that the pointer to a piece of memory in the address of XX; we say a pointer to a block of memory is equivalent to saying that the value of the pointer is the first address of the area of memory.

The memory area pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In example one, the type that the pointer points to is already there, but because the pointer has not yet been initialized, the memory area it points to does not exist, or is meaningless.

Later, every time you encounter a pointer, you should ask: What is the type of the pointer? What type does the pointer point to? Where does the pointer point to?

Memory area occupied by the pointer itself

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

The concept of memory occupied by the pointer itself is useful in determining whether a pointer expression is a left value.

Arithmetic operations on pointers

The pointer can add or subtract an integer. The meaning of this operation of pointers is not the same as the addition and subtraction of the usual numerical values. For example:
Case TWO:

 Char a[20]; 
 int *ptr=a; 
... 
... 
 ptr++; 

In the example above, the type of pointer ptr is int*, which points to an int, which is initialized to point to reshape variable a. In the next 3rd sentence, pointer ptr is added 1, which is handled by the compiler: it adds the value of the pointer ptr to sizeof (int), and in the 32-bit program, it is added to 4. Because the address is in bytes, the address that PTR points to is increased by 4 bytes from the address of the original variable A to the high address direction.
Since the length of the char type is a byte, the original PTR is four bytes beginning with the unit No. 0 of array A, which points to four bytes starting with unit 4th in array A.
We can iterate over an array with a pointer and a loop, and see examples:

Example three:

int array[20]; 
int *ptr=array; 
... 
The code that assigns values to an integer array is omitted here. 
... 
for (i=0;i<20;i++) 
{ 
 (*ptr) + +; 
 ptr++; 
} 

This example adds the value of each cell in an integer array to 1. Because each loop adds pointer ptr 1, each loop accesses the next cell of the array. Look again at the example:

Example four:

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

In this example, the PTR is added with 5, and the compiler does this by adding the value of the pointer ptr to 5 by sizeof (int), and the 32-bit program adds 5 times 4=20. Because the unit of the address is byte, the current PTR points to an address that moves 20 bytes to the high address direction than the address pointed to by the 5 ptr. In this example, the PTR before 5 points to the four bytes starting at Unit No. 0 of array A, plus 5, and PTR already points beyond the legitimate range of array A. Although this situation will be problematic in application, it is grammatically acceptable. This also shows the flexibility of the pointer.

If, in the example above, PTR is subtracted by 5, the process is much the same, except that the value of PTR is subtracted from 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.

To sum up, after a pointer ptrold plus an integer n, the result is that a new pointer ptrnew,ptrnew the same type as the Ptrold, and the type that the ptrnew points to is the same type as the Ptrold. The Ptrnew value increases the byte of n by sizeof (the type to which ptrold points) than the Ptrold value. That is, the memory area that the ptrnew points to will move the n by sizeof (the type that the Ptrold points to) to a higher address direction than the memory point that ptrold points to. A pointer ptrold minus an integer n, the result is a new pointer ptrnew,ptrnew the same type as the Ptrold, and the type that the ptrnew points to is the same type as the Ptrold. The value of the ptrnew reduces the byte of n by sizeof (the type that the Ptrold points to) than the Ptrold value, that is, the memory area that the ptrnew points to will move the N times ptrold (the type that the sizeof points to) than the memory area to which the ptrold points to the lower address direction Bytes.

Operators & and *

Here & is to take the address operator, * is ... The book is called an "indirect operator". The result of the &a operation is a pointer, the type of the pointer is the type of a *, the type of the pointer is the type of a, the address to which the pointer points, and that is the address of a. The results of *p's operations are all the same. In short, the result of *p is what p points to, something that has these characteristics: its type is the type that P points to, and the address it occupies is the address that P points to.

Example five:

int a=12; 
int b; 
int *p; 
int **ptr; 
The result of the p=&a;//&a is a pointer, the type is int*, the type that points to is int, and the address that points to is the address of a. The 
result of *p=24;//*p, where its type is int, the address it occupies is the address that P points to, and obviously *p is the variable A. The
result of the ptr=&p;//&p is a pointer, and 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, which is int*. The address that the pointer points to is the address of the pointer P itself. *ptr=&b;//*ptr is a pointer, and the result of the 
&b is also a pointer, and the two pointers are of the same type as the type they point to, so it's no problem to amp;b to give *ptr a value. The
result of the **ptr=34;//*ptr is the thing that PTR points to, here is a pointer, a second * operation on the pointer, and the result is a variable of type int.

Pointer expression

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

Example SIX:

int a,b; 
int array[10]; 
int *pa; 
Pa=&a;//&a is a pointer expression. 
int **PTR=&PA;//&PA is also a pointer expression. 
both *ptr=&b;//*ptr and &b are pointer expressions. 
Pa=array; 
pa++;//This is also a pointer expression.

Example Seven:

Char *arr[20]; 
Char **parr=arr;//if arr as a pointer, arr is also pointer expression 
char *str; 
Str=*parr;//*parr is a pointer expression 
str=* (parr+1);//* (parr+1) is a pointer expression 
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 elements that the pointer has: the type of the pointer, the type the pointer points to, the memory area the pointer points to, and the memory the pointer itself occupies.

Well, when a pointer expression's result pointer has explicitly had the memory occupied by the pointer itself, the pointer expression is a left value, otherwise it is not a left value. In Example seven, &a is not a left value because it does not yet occupy a clear memory. *ptr is a left value, because *ptr this pointer has occupied the memory, in fact, *ptr is the pointer pa, since the PA has its own position in memory, then *ptr certainly have its own position.

The relationship between arrays and pointers

If you don't quite understand the statement that declares the array, see the articles I posted earlier << how to understand complex type declarations >> of C and C + +. The array name of an array can actually be viewed as a pointer. Look at the following example:

Example eight:

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

In the example above, the array name array, in general, represents the array itself, the type is int [10], but if you think of an array as a pointer, it points to the No. 0 cell of the array, the type is int *, and the type of the array cell is the type of 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. Others, and so forth.

Example nine:

Char *str[3]={ 
"Hello,this is a sample!", 
"Hi,good morning.", 
"Hello World" 
}; 
Char s[80]; 
strcpy (s,str[0]);//may also be written as strcpy (S,*STR); 
strcpy (s,str[1]);//may also be written as strcpy (s,* (str+1)); 
strcpy (s,str[2]);//may also be written as strcpy (s,* (str+2)); 

In the example above, STR is an array of three cells, each of which is a pointer to a string. Referring to the pointer array named STR as a pointer, it points to unit No. 0 of the array, its type is char**, and the type it points to is char *.

*STR is also a pointer, its type is char*, the type it points to is char, and it points to an address that is the string "Hello,this is a sample!" The address of the first character, that is, the address of ' H '. Str+1 is also a pointer to unit 1th of the array, its type is char**, and the type it points to is char *.

* (str+1) is also a pointer, its type is char*, and the type it points to is Char, which points to "Hi,good morning." The first character ' H ', and so on.

Here's a summary of the array name problem. Declares an array type array[n], the array name array has two meanings: first, it represents the entire array, its type is kind [n], and second, it is a pointer, the type of which is type*, the pointer to the type, which is the type of the array cell, The memory area that the pointer points to is the array number No. 0 unit, which occupies a separate memory area, noting that it occupies a different memory area than the number No. 0 unit. The value of the pointer cannot be modified, that is, an expression similar to array++ is wrong.

Array name array can play different roles in different expressions.

In the expression sizeof (array), the array name array represents the array itself, so the sizeof function then measured the size of the entire array.

In the expression *array, the array acts as a pointer, so the result of this expression is the value of the array number No. 0. sizeof (*array) measured the size of the array cell.

Expression array+n (where n=0,1,2, .....) , the array acts as a pointer, so the result of the array+n is a pointer, its type is type*, and it points to the type, which points to the array nth unit. So sizeof (ARRAY+N) is measured by the size of the pointer type.

Example Ten:

int array[10]; 
int (*PTR) [ten]; 
ptr=&array; 

In the example above, PTR is a pointer whose type is int (*) [10], and the type that he points to is int [10], and we initialize it with the first address of the entire array. In the statement Ptr=&array, the array represents the arrays themselves.

The function sizeof () is mentioned in this section, so let me ask, does the sizeof (pointer name) measure the size of the pointer itself or the type the pointer is pointing to? The answer is the former. For example:

int (*ptr) [10];

In the 32-bit program, there are:

sizeof (int (*) [ten]) ==4 
sizeof (int [ten]) ==40 
sizeof (PTR) ==4 

In fact, sizeof (objects) measure the size of the object itself, not the size of the other type.

Relationship of pointer and struct type

You can declare a pointer to a struct-type object.

Example 11:

struct MyStruct 
{ 
int A; 
int b; 
int c; 
} 

MyStruct ss={20,30,40};//declares the structure object SS and initializes the three members of the SS to 20,30 and 40.
MyStruct *ptr=&ss;//declares a pointer to the structure object SS. Its type is
mystruct*, and the type it points to is mystruct. The
int *pstr= (int*) &ss;//declares a pointer to the structure object SS. But its type is different from the type it points to and the PTR.

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

Answer:

ptr->a; 
ptr->b; 
ptr->c; 

And how can I access the three member variables of SS through the pointer pstr?

Answer:

*pstr;//visited member a of the SS. 
* (pstr+1);//access to member B of the SS. 
* (pstr+2)//access to the SS member C. 

Oh, although I have raised the above code in my msvc++6.0, but you know, this use of pstr to access the structure of members is not normal, in order to explain why not formal, let's see how to access the array of cells through the pointer:

Example 12:

int array[3]={35,56,37}; 
int *pa=array; 

The method of accessing the three cells of array arrays via pointer PA is:

*pa;//visited unit No. 0 
* (pa+1);//visited Unit 1th 
* (pa+2); 

The format is the same as that of an irregular method that accesses a struct member through a pointer.

When all of the C + + compilers arrange array cells, they always store each array unit in a contiguous storage area, and there is no gap between the unit and the unit. However, when you store individual members of a struct object, in some kind of compilation environment, it may be necessary to add a number of "padding bytes" between the adjacent two members, which might require a word alignment or two-word alignment or something, which could result in several bytes of space between the members.

Therefore, in example 12, even if *PSTR accesses the first member variable A of the structure object SS, it is not guaranteed that * (PSTR+1) will be able to access struct member B. Because there may be a number of padding bytes between member A and member B, maybe * (pstr+1) Just accesses these padding bytes. This also proves the flexibility of the pointer. If your goal is to see if there are any padding bytes between the members of the structure, hey, that's a good way to go.

The correct way to access a struct member through a pointer should be a method using pointer ptr in Example 12.

The relationship between pointers and functions

You can declare a pointer as a pointer to a function.

int fun1 (char*,int); 
Int (*PFUN1) (char*,int); 
PFUN1=FUN1; 
.... 
.... 
int a= (*PFUN1) ("ABCDEFG", 7);//Call function via function pointer. 

You can use pointers as parameters for functions. In a function call statement, you can use a pointer expression as an argument.

The above is the C + + pointer data collation, follow-up continue to supplement the relevant information thank you all for the support of this site!

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.