In-depth understanding of C language pointers

Source: Internet
Author: User

Pointers are a widely used data type in the C language. The use of pointer programming is one of the most important styles of C language.

Pointer variables can be used to represent various data structures, easy to use arrays and strings, and can be processed as assembly language memory address, so as to compile a concise and efficient program. Pointers greatly enrich the functionality of the C language.

Learning pointers are the most important part of learning C, and the ability to correctly understand and use pointers is a sign of our mastery of C language.

Why does the C language have pointers and pointers and what are the benefits?

Broadly speaking: pointers are one of the main characteristics of C language that distinguishes it from other high-level languages. Second, because arrays or functions are stored continuously. The array or function is found by accessing the pointer variable to get the first address of the array or function. In this way, wherever an array is present, the function can be represented by a pointer variable, provided that the pointer variable is given the first address of the array or function. Doing so will make the concept of the program very clear and the procedure itself concise and efficient.

In the C language, a data type or structure often occupies a contiguous set of memory units. The concept of "address" does not describe a data type or structure well, while a "pointer" is actually an address, but it is the first address of a data structure, which is "pointing" to a data structure, so that the concept is clearer and more explicit. This is also an important reason to introduce the concept of "pointer".

Using pointers can bring the following benefits:

(1) It can improve the compiling efficiency and execution speed of the program, and make the program more concise.

(2) Through a pointer called function can return to the calling function other than the normal return value of data, so as to achieve two-way communication between the two.

(3) Dynamic memory allocation can be achieved by using pointers.

(4) Pointers are also used to represent and implement a variety of complex data structures, thus laying the groundwork for higher quality programs.

(5) The use of pointers can directly manipulate the memory address, so as to complete and assembly language similar work.

The pointer to the variable is the address of the variable. The variable that holds the variable address is a pointer variable. That is, in the C language, a variable is allowed to hold a pointer, a variable called a pointer variable. Therefore, the value of a pointer variable is the address of a variable or a pointer to a variable.

To represent the relationship between a pointer variable and the variable it points to, the "*" symbol in the program means "point to"

Define a pointer variable

The definition of a pointer variable consists of three contents:

    1. Pointer type description, which defines the variable as a pointer variable;
    2. pointer variable name;
    3. The data type of the variable that the variable value (pointer) points to.
int *p2;  /* P2 is a pointer variable that points to an integer variable */ float *p3;  /* P3 is a pointer variable that points to a floating-point variable */ Char *p4;  /* P4 is a pointer variable that points to a character variable */

It should be noted that a pointer variable can only point to a variable of the same type, such as a P3 can only point to a floating-point variable, cannot sometimes point to a floating-point variable, and sometimes point to a character variable.

Reference to pointer variable

Pointer variables, like ordinary variables, need to be defined not only by the description, but also by a specific value. An unassigned pointer variable cannot be used, otherwise it will cause system confusion and even panic. The assignment of a pointer variable can only be assigned to an address and must never be given any other data, or it will cause an error. In the C language, the address of a variable is assigned by the compilation system, completely transparent to the user, and the user does not know the specific address of the variable.

Two related operators:

      • &: Take address operator;
      • *: pointer operator (or "indirect access" operator).

There are two ways to assign the address of an integer variable A to p if you have a pointer variable p that points to an integer variable:

1. How to initialize pointer variables:

1 int A; 2 int *p=&a;

2. How to assign a statement:

int A; int *p;p=&a;

It is not allowed to assign a number to a pointer variable, so the following assignment is wrong:

int *p;p=+;

Accessing a variable pointed to by a pointer is done in the form of an indirect access, so it takes time and is not intuitive to access a variable directly, because which variable to access by the pointer depends on the value of the pointer (that is, the pointing), such as "*P2=*P1;" In fact, "J=i," the former is not only slow and unclear. but because pointers are variables, we can indirectly access different variables by changing their direction, which gives the programmer flexibility and makes the program code more concise and efficient.

In-depth understanding of C language pointers

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.