C and pointer (pointers on C) -- Chapter 6: pointer (top)

Source: Internet
Author: User
Tags float double
Chapter 6 pointer


This chapter clearly highlights the sensitivity of beginners. I will also be unfamiliar with this part after a period of time without using C. Well, it is actually unfamiliar with the high-level pointer.
Of course, there are always a lot of references in this part, such as the problem of array range, or I will not abuse myself to read such books in the summer.


Summary:
The value of the pointer variable is not the value stored in the memory location to which it points, but the address of the variable to which it points is saved. You need to access the value of the variable pointed to through the indirect access character.
Instead of initializing a pointer, no memory is allocated. Therefore, the pointer must be initialized before indirect access is performed.
Either point to a variable or malloc a memory.
A null pointer is a pointer that does not point to anything. Apart from the NULL pointer, there is no built-in notation to identify pointer constants. Unless it is named volatile 0x00..., this is unimaginable in the PC system. In rare cases, we occasionally need pointer constants.
A pointer can be used as a left value because it identifies a specific memory location.
For example, PTR = & para;
* PTR = para;
Some limited arithmetic operations can be performed on Pointer values. You can add an integer value to a pointer, or subtract an integer value from a pointer. In both cases, the integer value is adjusted, and the original value is multiplied by the length of the target type of the pointer.
Note that the pointer size is a byte. Therefore, add 1 to A pointer and point it to the next space. This is not related to the variable float double.
Mutual operations between pointers can only be predicted for the results in the array. For non-array element operations, it is better not to use them. It must be illegal, but ide does not report an error.
When Subtraction is performed, if the pointer points to the position before the first element of the array, it is invalid.
During addition, if the pointer points to a position after the last element of the array, it is valid, but it is not valid. Do not touch this bottom line.
If both pointers point to the elements of the same array, they can be subtracted to indicate the number of elements in the array.
If the two pointers do not point to the same array, their Subtraction is incorrect.
For relational operations, the simplest and most common is to test whether they are equal or not.
If both pointers point to the elements of the same array, you can also use the >=, >,<,< = elements.


Warning:
1. an uninitialized pointer variable is incorrectly referenced.
Int *;
...
* A = 12;
A points to a location. If a is static, it will be initialized to 0. If a is automatic, it will not be initialized. In either case, declaring only one pointer variable does not open up a memory space.
Window is gentle. Generally, it is to terminate the program. When it is unlucky, it will generate a general protection exception ). In UNIX, it is called memeory fault, which prompts the program to try to access a location not allocated to the program memory.
Be sure to make sure they have been initialized when calling pointers.
2. incorrectly dereference a null pointer.
Int *;
A = NULL;
Temp = * A; // wrong!
3. incorrectly pass the NULL pointer to the function.
This error is purely deserved. You must check the function validity before passing it.
4. No pointer expression error is detected, leading to unexpected results.
For example, when declaring a pointer, if you already know the address to be initialized, initialize it. Otherwise, the initialization is null, which is a good thing and greatly saves debugging time.
5. Perform a subtraction operation on a pointer so that it illegally points to the memory position before the first element of the array.
See!


Programming tips:
1. A value should only have one meaning.
2. if the pointer does not point to anything meaningful, set it to null.


Question?
5. Int I [10];
Int * P = & I [0];
Int offset;
P + = offset; ()
P + = 3; (B)
What is the difference between A and B?
A: There is no difference in the evaluation process, but a checks whether the array of multiple steps is out of bounds.
6. Int array [array_size];
Int * PI;
For (Pi = & array [0]; pI <& array [array_size];)
* ++ Pi = 0;
Where is the error?
A: The objective is to clear the array, but array [0] is not cleared because of the prefix of ++. At the same time, a zero is cleared, and a memory space behind array [ARRAY_SIZE-1] is also cleared.
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.