[C Basics] data type-array

Source: Internet
Author: User
1. One-dimensional array 1. array name

The value of the array name is a pointer constant, so you cannot use a value assignment to copy the array. (The constant cannot be the left value. The left value should be the equivalent of the address)


Two exceptions: When the array name is used as the operand of the sizeof operator. (The result is the length (in bytes) of the entire array. (The result is a pointer to the array) eg:
Int A [10]; int (* P) [10] = & A; // P is the pointer to the array, & A is the pointer to the array 2. subscript references have the same priority as indirect access. (Subscript is higher than indirect access) eg: (subscript reference)
Array [subscript] (indirect access)
* (Array + (subscript) Special syntax
Subscript [array] is equivalent to the above three
The C subscript check involves a lot of overhead. Therefore, the subscript check is generally not performed. Generally, programmers need to perform the check on their own.
3. pointer and subscript
In terms of readability, the subscript is better (especially for multi-dimensional arrays), and the efficiency of pointer access may be higher.
Pointer efficiency: when moving in an array based on a fixed number of increments, using pointer variables produces more efficient code than using subscript. Pointers declared as registers are generally more efficient than pointers in static memory and stacks. Program maintenance is the main cost of software products.
4. array name as function parameter
All parameters passed to the function are implemented by passing values. When an array name (pointer) is passed, a copy of the object is passed. The modification to the copy does not change the real parameter itself, however, through indirect access, we can modify the content to which it points (equivalent to side effects ). The effect shown in this way is called by the same transfer address.
5. Declare array parameters
When a function is called, a pointer is actually passed, and the function parameter is actually a pointer. Eg: The following function prototype is equivalent to int strlen (char * string) int strlen (char string []). Note: array parameters can match arrays of any length, to pass the array length, you must pass the length as a display parameter to the corresponding function.
6. Initialization
Static initialization: array elements are fully initialized to 0 by default, and array elements are not initialized by default. For incomplete initialization, the following elements are automatically filled with zero; the array length is not provided during initialization, which is automatically calculated by the compiler. Character array initialization, which can be assigned a value using a String constant. In this case, the string constant is automatically interpreted as the initialization list.
Ii. Multi-dimensional array
1. Storage Sequence
The storage order of elements in a multi-dimensional array changes first according to the rightmost subscript, which is called the row primary order.
A [2] [3]
A [0] [0] A [0] [1] A [0] [2] A [1] [0] A [1] [1] A [1] [2]

Actual storage mode of data units in memory
The two-dimensional array name is a pointer to an array, defined as: int matrix [3] [5]; int (* P) [5] = matrix;
// P is the pointer matrix + 1 to an array containing five int elements.
// Pointer to an array containing five integer elements, pointing to the second row of matrix * (matrix + 1)
// Pointer to integer x (matrix + 1) + 5
// Pointer to an integer *(
* (Matrix + 1) + 5
) // Integer

// Pi is defined as an integer pointer int * Pi = & matrix [0] [0]; int * Pi = matrix [0];
2. Multi-dimensional array as function parameter
When a multi-dimensional array is used as a function parameter, the length of other dimensions except the first dimension must be declared (subscript must be used to calculate the length) eg: void fun (INT (* mat) [10]); void fun (INT mat [] [10]); void fun (INT ** mat );
// Error. The pointer to the integer pointer is different from the pointer to the integer array.
3. Initialization
Only the first dimension can be provided by default based on the initialization list.
3. pointer Array

Eg: int * API [10];
// API is an array containing 10 elements. The element is an integer pointer (pointer array) int (* API) [10];
// API is a pointer pointing to an integer array containing 10 elements (pointing to an array pointer)








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.