Interesting things in C Language _ indirect reference and parsing reference

Source: Internet
Author: User

In the C language, the use of variables is essentially access to the content stored in the computer memory, and write and read through reference to the memory space. (Note: C has a special keyword register,

It is used to declare variables that are not stored in memory. register is used to store variables in computer registers. The main purpose of such variables is to speed up CPU access.) The register keyword is

The outcome of a specific period is that the variables defined by register are indeed stored in computer CPU registers when memory access is slow. Now, because the memory access rate is fast, many compilers

During compilation, the register variable is also stored in the memory. Here we will mainly discuss introduction and resolution references, so we will not discuss register any more.

Indirect reference refers to accessing another memory space through a register or a memory space instead of directly pointing out the memory address in the assembly language instruction.

Exp:

MoV ax, [BX]

In this way, an indirect reference is implemented. This statement does not directly access the Bx register, but access the memory space pointed to by BX. In C, we use pointers for indirect reference.

Exp:

Int var1;

Int * pvar = NULL;

Pvar = & var1;

* Pvar = 10;

In this way, an indirect reference is implemented, and one memory space is used to access another memory space, so indirect reference is implemented. Indirect reference is also called indirect addressing in the computer world.

The preceding C language may be translated into assembly instructions in the following form:

MoV [ax], 10; The content stored in ax is the memory address of the variable var1, which may also involve the offset calculation of the physical address.

The biggest difference between an assembly language and a C language is that it can access registers explicitly and directly calculate a memory address. Other C language control structures such as loop statements can be implemented in the Assembly,

You can explicitly specifyProgramThe starting address of the execution, which can be implemented through the org command, will not be discussed.

1. indirect reference

We can look at the indirect reference through several instances:

Int var1;

Int var2;

Int * pvar1 = NULL;

Int ** pvar = NULL;

Pvar1 = & var1; // a required condition for indirect reference. The address of a memory space is put into another memory space through the & operator.

Pvar = & pvar1; // indirect reference. Because pvar1 is also a variable and pointer variable, there is a corresponding memory space in C,

* Pvar1 = 10; // The literal value is placed in the memory space pointed to by * pvar1,

Pvar = & var2, // This is incorrect. Although we can make it run smoothly in some ways, if we do so, unknown errors may be introduced.

What should I do if I really want to indirectly reference var2 through pvar? It is the same as the following:

* Pvar = & var2;

Then reference var2 as follows;

* (* Pvar) = 10; // var2 = 10

2. parsing references

I don't know if this is appropriate, but * when I perform an operation on the address, many authoritative documents say this, so I will follow it. In fact, * There is nothing to explain.

Specifically, it is:

* The extracted content is stored in one memory space area (memory area 1) and interpreted as another memory area (memory area 2 ). Then we can

Access in memory Zone 2.

If multiple parsing operations are performed, more memory areas will be involved, as mentioned above:

Exp:

Int var1; // var1 memory space 1, which can be simply understood as the var1 identifier pointing to memory space 1, although we did not explicitly point this point

Int var2; // var2 memory space 2

Int * pvar1 = NULL; // pvar1 memory space 3

Int ** pvar = NULL; // pvar memory space 4

Pvar1 = & var1; // place the location of memory space 1 in memory space 3, forming a pointing relationship

Pvar1 = 10; // place the integer literal value 10 to memory space 1 pointed to by memory space 3.

* Pvar = & pvar1, // form a multi-point, pvar points to pvar1, and ** pvar points to memory space 1

3. about defining the position of the pointer variable parsing Operator

Exp:

Int * P;

And

Int * P;

The above two definitions are essentially the same, but more variables may be misunderstood.

My habit is that each variable definition occupies a single row:

Exp:

Int * P1,

* P2;

This makes it clear.

If typedef is involved, there will be another situation,

Exp:

Typedef int * int;

Int var1;

Int * var2;

Unfortunately, many people make mistakes in this place, and I have also made mistakes. I originally wanted to declare a pointer var2 pointing to the int variable. The result is a pointer pointing to the int * type.

Variable.

My habits are:

1. When a non-typedef is defined, the * operator is near the variable.

2. When typedef defines a new type identifier *, it is close to the original type identifier.

Exp:

Int * P, * PP;

Typedef int * pint;

In this way, the intention is obvious, and it is not easy to cause misunderstanding.

4. Four arithmetic operations on pointers

Adding two pointer variables is undefined,

The result of the two pointer variables to be subtracted returns the distance between the two Memory Spaces,

Two pointer variables are commensurate with undefined behaviors,

Division of two pointer variables is undefined,

The addition and subtraction of pointer variables and integer values is the position to which the pointer moves.

The division of pointer variables and integer values is undefined.

Exp:

Size_t (strlen) (const char * Str)

{

Char * start;

Start = STR;

While (* start)

{

Start ++;

}

Return (START-Str );

}

5. Understand pointers

The most important thing to understand pointers is the concept of pointing. If you simply understand them from the perspective of variable storage, you may encounter many questions.

Pointing to this word is really special. The key is to understand indirect references.

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.