Pointers on C _ Reading Notes (1)

Source: Internet
Author: User
1. Quick Start
Logically, it is better not to use comments to delete a piece of code, but to use [# If 0 statements # endif]
If some declarations need to be used in several different source files, you can put the corresponding declarations in the same header file to avoid duplication in multiple places for easy maintenance. Use # include to introduce the header file.

2. Basic Concepts
3. Data
4. Statements

C does not have a special value assignment statement. For example, [x = Y + 3;] is an expression rather than a value assignment statement, because it is legal to directly write [Y + 3.
C does not have a boolean type, but is replaced by an integer. The zero value indicates "false", and the non-zero value indicates "true ".
The else clause belongs to the incomplete if statement closest to it.
A For loop can be written as a while loop, but the two are different when they encounter a continue. A for loop does not skip the condition adjustment section, while a while loop does. The second is that the statements of the for loop are all in one place, while the while loop is dispersed.
5. Operators and expressions
<And>: Left shift operator. The bitwise null given by the right side is filled with zero. Right Shift: Logical shift and arithmetic shift. If the logical shift is performed, null out-of-the-box Fill is performed. The movement of the bitwise left by the arithmetic shift is determined by the symbol of the original value. The compiler determines whether to execute logical shift or arithmetic shift, so >> means the program cannot be transplanted.
6. pointer
It is a good analogy to use a pointer as a door number. However, when explaining the pointer to the pointer, the limitations of this metaphor are reflected, because the content of the House cannot be a house number.
The type of a value is not determined by the value, but by the usage of the value.
Note [int * A; * A = 12;] for this type of error, you need to develop a habit of ensuring that the pointer has been initialized before indirect access to the pointer.
The pointer can be used as the left value, and indirect access to the pointer can also be used as the left value, because it identifies a clear memory location.
Instance:

Code
1 /**//*
2 ** given a pointer to the pointer list ending with null, you can find a specific character in the string in the list.
3 */
4 # inlcude <stdio. h>
5
6 # define true 1
7 # define false 0
8
9int find_char (char ** strings, char value)
10 {
11 char * string;/** // * the string we are currently searching */
12
13/** // * for each string in the list... */
14 While (string = * strings ++ )! = NULL)
15 {
16/** // * observe each character in the string to see if it is the one we need to find. */
17 while (* string! = '\ 0 ')
18 {
19 if (* string ++ = value)
20 {
21 return true;
22}
23}
24}
25 return false;
26}
27
28

Pointer operation:The pointer algorithm does not depend on the pointer type. For example, a char pointer p, p + 1 points to the next Char. If P is a pointer to the float type, P + 1 points to the next float. Because the pointer and an integer are adjusted according to the appropriate size before the addition. For example, if float occupies 4 bytes, then P + 3, the integer actually added to the pointer is 12, that is, P + (3*4)
Second, the arithmetic operation of the pointer is limited to two forms,
1st: pointer + or-integer.
Type 2nd: pointer-pointer. Subtraction is allowed only when two pointers direct to two elements in the same array. The result is a signed integer value, indicating the distance between the two pointers in memory (in units of the length of the array element, not in bytes, because the result of the subtraction operation is divided by the length of the array element type ). The pointer pointing to different array elements is not significant, so programmers should avoid this situation.
The relational operations (<,>, >=, <=) of pointers are the same as those of pointer subtraction. relational operations of pointers are meaningful only when they point to elements in the same value.

7. Functions
ADT and black box: This is not very clear, to be supplemented...
Recursion: the recursion overhead is relatively large, but sometimes it uses Recursion to make the code clearer. For example, when using recursion to calculate factorial and Fibonacci numbers, the readability is not dominant compared with iterative loops, the performance is far from each other, so we can fully consider using iterative loops.

8. Array The array name usually indicates a pointer constant, but it is not expressed by a pointer constant in two cases, that is, when it is used as the sizeof operator or the operand of the single object operator. Sizeof returns the length of the entire array, rather than the length of the pointer to the array. The address operator is used to obtain a pointer to an array, rather than a pointer to a constant value of a pointer. Pointer and subscript: The pointer is not more efficient than the pointer, but the pointer is sometimes more efficient than the subscript. Let's look at the following two short programs: Use subscript: Int array [10], A; for (a = 0; A <10; A + = 1) array [a] = 0; Use Pointer:Int array [10], * AP; For (AP = array; A <array + 10; AP ++) * ap = 0;

Multiplication is involved in both methods of code. One is to obtain the value of A and multiply it by the integer length (4) When the subscript expression is evaluated. The next AP ++ also involves multiplication, that is, multiplication of 1 and integer length. The difference is that the multiplication by pointer is only executed once during program compilation, this is not the case when subscripts are used.
Storage of multi-dimensional arrays, such as int array [3] [6], the storage format is [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] ], it is important to remember this, and it is related to the result of a pointer to an array element before performing arithmetic operations.
Array pointer: int matrix [3] [10], * PM = matrix; this statement has a problem. matrix is actually a pointer array pointing to 10 elements in row 1st, the famous pointer array should adopt the following method:
Int {* pm} [10] = matrix;
Pay attention to the differences between pointer arrays and matrices. For example, when storing many strings, you can think of different advantages of the two.
9. String
Strlen returns a value of the size_t type and an unsigned integer. So if (strlen (x)-strlen (y)> = 0) is unexpected, because the value of the unsigned type cannot be negative, therefore, the statement is always true. This problem can be eliminated if the strlen return value is forcibly converted to int.
When copying a string, the programmer must ensure that the space of the target character array is greater than the length of the Source Target string, because if redundant characters occur, the redundant characters will still be copied and overwrite the memory behind the target array, strcpy cannot solve this problem. It is dangerous to overwrite unknown memory values.

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.