Precautions for pointers in C Language

Source: Internet
Author: User

Precautions for pointers in C Language

I. Four key concepts of pointers
1. pointer type
2. pointer type
3. the pointer value, that is, the address pointed to by the pointer
4. memory space occupied by the pointer
Note: The content stored in the pointer variable is the address number of the memory!

For example:
Int ** pp = NULL;
1. the pointer type is int **
2. int type pointed to by pointer *
3. the pointer value is NULL.
4. sizeof (pp)

Ii. Forced type conversion
The forced type does not change the binary arrangement and sequence in the memory. It only interprets the binary as the target type.
For example:
Int I = 10;
Binary 0110;
Float f = (float) I;
The binary value of variable f is 0110.

3. Modify the CONST variable
When you look at the const variable, you can turn a blind eye to the data type name.
For example
Int const * p; // modifier * p, p can be changed, * p cannot be changed
Const int * p; // modifier * p, p can be changed, * p cannot be changed
Const int * const p; // modify p and * p, p is immutable, * p is also immutable

Iv. variable types
1. data types are essentially aliases with fixed memory size
2. The essence of a variable is the alias of the starting address of a continuous memory space.

V. Significance of variable Declaration
1. Create a variable symbol table
By declaring variables, the compiler can create a variable symbol table. As a result, the compiler is very clear about how many variables are used in the program and what the type of each variable is, the compiler can find out during compilation. This helps developers avoid writing variable names incorrectly due to negligence.
2. The data type of the variable indicates the amount of memory space allocated to the variable.
3. The data type of the variable indicates how the system interprets the values in the bucket. For the same value, different types may have different interpretations. Int occupies 4 bytes, float also occupies 4 bytes, And the binary number is also stored in the memory, and the binary number does not indicate whether the current type is int or float. How to differentiate? It is differentiated by the Data Type of the variable. Since the Declaration creates a variable symbol table, the system knows how to explain the variables.
4. The data type of the variable determines the value range of the variable.
For example, the short integer data value is-32767 ~ Between 32767
5. Different data types have different operations.
For example, an integer can be used to calculate the remainder. The C language uses the symbol "%" to calculate the remainder. An integer is acceptable. A real number is not allowed.

Vi. Summary of pointer passing through function calls
1. If you want to modify the value of a variable in the main function, you must pass the address (pointer) of the variable in the main function to the called function.
2. If the main function needs to pass a large data to the called function, you can also pass the address (pointer) of the variable of the main function to the called function, this will improve the performance of the program.
3. The N-level pointer is passed to modify the value of the N-1-level pointer,
For example, int * p. If you want to modify the value of p, you must pass & p to the function to modify the value of p.

VII. Exchange of pointers and Arrays
In expressions, pointers and arrays can be exchanged because they form pointers in the compiler and can all perform subscript operations.

8. Differences between arrays and pointers
Array: An array is an address, and the address is a constant value and cannot be changed. Therefore, the array name cannot be used as the left value.
Pointer: A pointer is an address, and the address can be changed. It can also be used as the left value.

IX. Differences between pointers and addresses
The pointer is the address, but the address is not the pointer. The pointer has a type and the address has no type.

10. Differences between data transfer and data transfer
In fact, there is only one way to pass values in C language, that is, pass by value.
The difference between passing by value and by address is that the method used in the function is only. If you want to modify the value that the address points to memory in the function, you can modify the content that the real parameter address points. Because the memory address is unique in the memory, you can use the address to modify the value in the memory wherever it is located.


Pointer variable Summary

 

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.