4) C language pointer (c self-learning)

Source: Internet
Author: User

pointer and pointer variables

The pointer is the address, and the address is a data type. Pointer variables are also variables, but only data of address types can be referred to as "address type" variables.

  1) internal deposit and address

When a program runs, the program itself and the data used in the program are stored in the computer's internal memory. Internal memory is composed of a plurality of internal storage units (bytes), each of which has its own unique, different address, called the Memory unit address.

  2) variable and address.

Char ch= ' A ';  Short S = 5;    float F; They are 1 bytes, 2 bytes, and 4 bytes respectively. (i.e., the internal deposit element)

If such an assignment statement "F=S+CH;" is present, the operation process is:

1. In the variable address comparison table, find the variable A, take out the address of S (2010), considering that its data type is a short integer, so from the beginning of the address of the 2 bytes to remove 1 integers (5);

2. According to the same method to remove the character ' A ' in the variable ch (want to be an integer 65), the added value of the expression is 70.

3. Then the variable and address table to find the address of the variable F, considering that its data is single-precision, so the result 70 is converted to a single precision of 70.0, and then into the corresponding 4 bytes of the variable F.

The above operation, through the variable name to find the address of the variable, and then from the byte of the corresponding address of the variable to get the value or a value into the variable corresponding to the byte of the address is called the direct access, because the address can play the role of finding "Operation Formation", like a pointer to the "operand", so the address is often called

  3) Array and address

In addition to variables, the program can also use arrays to hold data. Each array element also occupies a contiguous memory unit. An array occupies a total number of bytes equal to the length of the array multiplied by the number of bytes consumed by each array element.

When the source program compiles, each time an array is encountered, the memory unit is allocated by its data type and length, and the array name, data type, array length, and the first address of the array are recorded in the array and Address table.

For example, this statement is short a[3]={1,2,3};

  

Locate the array A in the array Address table, take the first address, and calculate the address of the group element A[0] by formula: 3210+0*2=3210, remove the integer 1 from the 2 bytes (3210, 3211) starting at that address, and remove the array element a[2 the same way] (address is 3210+2*2 =3214) in the integer 3, to add, to obtain the result 4. Then the address of a[1] is calculated by the formula: 3210+1*2=3212, and the result 4 is stored in the 3212 start 2 bytes (3212, 3213). In the above operation, through the table to obtain the first address of the array, by calculating the address of the element to obtain the address of the element, and then through this address to obtain the value of the element, or the way to deposit the value of the element is also a direct access mode.

  4) pointer variable

First, the address of the variable to be accessed into a "special variable", and then through the value of the address of the "special variable" to access the value of the variable, this access method is implemented by a "special variable" to store the address, so called the indirect access mode.

The "special variable" that holds the address value is the pointer variable.

Use pointer variables:

The assignment statement "A[1]=i*j" is completed by means of indirect access. The operation. The addresses of variables I, j and array elements a[1] are deposited in the pointer variable p_i, P_J, p, respectively. The following procedure is done as follows: First remove the address of I from the pointer variable p_i, and then remove the value of the variable I from the memory address, and then, by the same method, the value of the variable J is p_j by the pointer variable, and the address of the array element A[1] is taken out from the pointer variable p

How pointer variables are used

1) Assigning values to pointer variables

The format is: pointer variable = address type expression.

The operator that takes the address is "&", the operand is a variable or array element name, and the result is the address of the corresponding variable or array element.

For example: The address of the variable I should be written as the address expression "&i", the address of the array element a[1] should be written as an address expression "&a[1".

    Note: Although the address is an integer, it is not allowed to treat integers as "address constants" in the C language. So the "address type" expression cannot be an integer.

For example: int i, *p_i

P_i = &i//causes the pointer variable p_i to point to the variable I, the &i of which is an address type expression.

p_i = 100; An attempt to make the pointer variable p_i point to a memory unit with address 100 is wrong.

2) Use the pointer variable name directly.

When you need to use an address, you can directly reference the pointer variable name.

Example: int i,j, *p=&i,*q;

Q=p; Assigns the value of the pointer variable p (address of I) to the pointer variable Q.

scanf ("%d,%d", q,&j); Use the pointer variable q to accept input data into the variable I that points to Q.

3) Reference the variable it points to by using a pointer variable.

Use the format: * pointer variable name.

Example: int i =1,j=2,k,*p=&i;

K=*p+j; Since P points to I, *p represents I, and the result K equals 3.

  Note: The operand of the address operator must be a variable or array element that has been defined, but it cannot be an array name. The result of the operation is the address of the operand. For example, define variable A, array b[10], then &a,&b[0],&b[9] are valid, representing the address of the variable a address, array element b[0], the address of the array element b[9], and &b is wrong.

(not finished ....) )

4) C language pointer (c self-learning)

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.