The understanding of the pointer in C language and its basic use instance _c language

Source: Internet
Author: User

C language pointer, the key meaning is "point".

What does "mean" mean?
In fact, it can be understood as the meaning of instruction. For example, there is an object that we call a. It is this object, with such a title, that we are able to carry out a series of exchanges with the entity that is detached from the object. The instruction of an object is an abstraction of the object. With this abstract ability, there is the so-called wisdom and civilization. So this is the power of the abstract method of "instructing".

A pointer to a C language is a pointer to a data/instruction (in the Vonnois Mann system, the two are interlinked, in the same space). This is the indication, which is the starting position of this data/instruction. But data/code is a way to explain. For example, 0x0001, as an integer, can also be used as a series of instructions, but also as a string of characters, in short, how to explain all.

The C language, in the compile phase, determines the "interpretation method" of this data/instruction.
An integer pointer, for example, represents an explanation that can be interpreted as an integer, starting at the point where the pointer p is pointing.
A function pointer that can be interpreted from the point where the pointer p points to, interpreted as an instruction, the corresponding input and output, and the return value according to the type of function pointer, in accordance with the corresponding requirements.

In summary, the essence of C language is a pointer, but the pointer is not just the essence of C language, it is the essence of abstraction. There are similar things in every language, such as functions, such as references.

(References and pointers to the difference, my understanding, can not be a + +-offset operation of the pointer, is a reference.) Arbitrary offset, it is easy to make the target location does not conform to its corresponding meaning, resulting in failure to explain, and then crash. The advantage of adding a pointer to the offset function is to facilitate the presentation of a stack of data/instructions of the same type, such as an array of examples. )

A pointer to the same void type is also a feature of the C language. A null pointer, which removes a pointer of the specified type, so that the pointer can be interpreted in an arbitrary way, which poses a potential problem. But it can also be said that the special power of the C language (I generally understand the power of C language as this). The benefits of this offer are very flexible. Because you can use a uniform type to represent all types of data. The problems that are posed are similar to the above. That is, if the interpretation method is improper, it will have disastrous consequences. The C-language coercion type conversion is also a routine interpretation of the pointer. It can also cause problems.

Let's take a look at some basics about pointers:

1. The basic

int i = ten; 
int *p = &i; /* Defines a pointer p to the INT type and assigns the address of I to it 
/printf ("i=%d, &i=%p, p=%p, *p=%d \ n", I, &i, p, *p); 

The program output is:

i=10, &i=0x22ac44, P=0X22AC44, *p=10

&i is the address of I, the pointer P holds the address of I, *p is the value of the pointer, which is the value of I.

2. Parameter and return value of pointer type

/* Define a function that returns a pointer to the type int 
/int *swap (int *px, int *py) 
{ 
 int temp; 
 temp = *px; 
 *px = *py; 
 *py = temp; 
 return px; 
} 
int main (void) 
{ 
 int i = ten; 
 int j =; 
 int *m = Swap (&i, &j); 
 printf ("i=%d, j=%d, *m=%d \ n", I, J, *m); 
 return 0; 
}

The program output is:

I=20, j=10, *m=20

return px is equivalent to defining a temporary pointer of type int to save PX, and then assigning the pointer to the pointer m, so M's point and PX are the same.

3. Pointers and Arrays

int a[5] = {1, 2, 3, 4, 5}; 
int *PA = &a[0]; 
printf ("*pa=%d pa=%p a=%p \ n", *pa, PA, a); 
pa++; 
printf ("*pa=%d \", *PA); 

The program output is:

*pa=1 pa=0x22ac28 a=0x22ac28
*pa=2

When an array name is passed as a parameter, the actual pass is the pointer to the first element, as can be seen from the input above.
The pointer pa++ is to have the PA point to the next element.

4. Pointers and const

int n =; 
const int *x = &n; 
int const *Y = &n; 
printf ("*x=%d y++=%p \ n", *x, y++); 

Program output:

*x=30 y++=0x22ac1c

the const int and int const are the same, both defining a pointer to the const int type. So *x is immutable and cannot perform operations such as (*X) + +, but x is variable and can perform x + + operations.

int * Const Z = &n; 
printf ("+ + (*z) =%d \ n", + + (*z)); 

Program output:

+ + (*z) =31

The above definition is a const pointer to the type int, so z is immutable, but the value of the pointer is variable. The only immutable pointers to define are:

int const * Const PZ;

5. Pointer to pointer

int c =; 
int *pc = &c; 
int **PPC = &pc; 
printf ("&pc=%p ppc=%p *ppc=%p **ppc=%d", &pc, PPC, *PPC, **PPC); 

Program output:

&pc=0x22ac0c ppc=0x22ac0c *PPC=0X22AC10 **ppc=40

*PPC takes the value of the PC, and **PPC is the equivalent of *PC, which is the value of C.

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.