Chapter 8 array
The intention of the author in this chapter is mainly to compare with the pointer, the similarities and differences between the two. Because my younger brother has a compilation base of 0, I really don't understand the efficiency of the 8.1.4 pointer.
Arrays learned in undergraduate courses will never learn arrays as they are now!
Can it be mixed with a pointer when it is passed as a function parameter?
Are declared arrays and declared pointers the same during compilation?
The form parameter can omit several subscripts. Why?
Char * A = "hello"; what is the difference with Char A [] = "hello?
What is the difference between a pointer to an integer pointer and a pointer to an integer array?
Summary:
In most cases, the value of the array name is a pointer to the first element. Except sizeof: The returned value is the byte occupied by the entire array. &: Returns a pointer to an array, which is not a pointer to the first element.
The subscript expression array [value] is the same as the indirect access expression * (array + (value. Therefore, subscript can be used not only for Array names, but also for pointer expressions.
The pointer expression is more efficient, but the maintainability of the code is more necessary. Therefore, it is better to express it more clearly.
When the pointer is declared, the value is allocated space to hold the pointer.
An array is actually a pointer constant. When we declare an array, it also allocates some memory space.
When the array name is passed as a function parameter, a pointer pointing to the array's 1st elements is actually passed to the function. The parameter received by the function is actually a copy of the original parameter, so the function can operate on it without affecting the actual parameter.
When the pointer parameter is passed as a function parameter, the original parameter can be modified.
But when the function is declared:
Int strlen (char * string );
Int strlen (char string []); this is equal. But this is the only way to be equal.
Initialization:
An array can be enclosed by a pair of curly braces as an initial value list. When the list value is less than the array length, the default value is used for initialization.
A string array can also be initialized using a fast method similar to a String constant.
Char * A = "hello"; and char a [] = "hello ";
The former is obviously a String constant. The latter "" only represents an array initialization list.
The storage order of multi-dimensional arrays is the first change to the rightmost subscript.
The value of a multi-dimensional array name is a pointer to its first element, that is, a pointer to an array.
When an array name is passed as a parameter to a function, the length must be explicitly specified after the first dimension. In this way, the compiler can infer the length of each subarray dimension.
Pointer array:
Two methods. First, the matrix format:
Char keyword [] [10] =
{"Do ",
"",
"If"
};
In this way, the matrix will contain the NUL Terminator. The length of each row is the same.
Char keyword [] =
{
"Do ",
"",
"If ",
'\ 0'
};
In this way, this is an array of pointers. The length of each row is automatically adjusted, and the size and space of the array will be increased.
Warning:
1. When accessing multi-dimensional array elements, misuse commas to separate subscripts:
Int A [3] [4]; // right
Int A [3, 4]; // actually only declares a [4]
2. Execute pointer operations on an array pointing to an unexplicitly declared length.
Int matrix [3] [10];
Int * P = matrix; // error. matrix is not a pointer to an integer, but a pointer to an integer array.
It should be declared as follows: int (* P) [10] = matrix;
Programming tips:
1. It is obviously better to write good code at the beginning than to rely on the compiler to correct bad code.
2. the readability of the source code is almost always more important than the program running timeliness;
3. Whenever possible, pointer parameters of the function should be declared as Const.
Three reasons: 1. Better document habits. Some people want to observe the function prototype and find that the data cannot be modified.
2. the compiler can capture any errors that may occur when it tries to modify the data.
3. Such declarations allow passing const parameters to functions.
4. Use full multi-layer curly braces in the multi-dimensional array's Initial Value List to improve readability.
C and pointer (pointers on C) -- Chapter 8: array (top)