Chapter 5. Relationship between arrays and pointers

Source: Internet
Author: User
Chapter 5. Relationship between arrays and pointers 1. array names can be considered as pointers. See the following example: Example 1: int array [10] = {,}, value; value = array [0]; // It can also be written: value = * array; value = array [3]; // It can also be written as: value = * (array + 3); value = array [4]; // It can also be written: value = * (array + 4); In the above example, the array name array represents the array itself, and the type is int [10]. However, if we regard array as a pointer, it points to the 0th units of the array. The type is int *, and the type is the type of the array unit, that is, Int. Therefore, it is not surprising that * array is equal to 0. Similarly, array + 3 is a pointer to the array's 3rd units, so * (array + 3) is equal to 3. Others. Ii. definition and use of pointer array 2: char * STR [3] = {// note the definition and initialization format of pointer array "Hello, this is a sample! "," Hi, good morning. "," Hello World "}; char s [80]; strcpy (S, STR [0]); // It can also be written as strcpy (S, * Str ); strcpy (S, STR [1]); // It can also be written as strcpy (S, * (STR + 1); strcpy (S, STR [2]); // It can also be written as strcpy (S, * (STR + 2). In the preceding example, STR is an array of three elements, and each element of the array is a pointer, each Pointer Points to a string. If the pointer array name STR is used as a pointer, it points to the cell 0th of the array. Its type is Char **, and it points to char *. * STR is also a pointer. Its type is char * and it points to Char. It points to the string "Hello, this is a sample! ", That is, the address of 'H. STR + 1 is also a pointer pointing to unit 1st of the array. Its type is Char **, and its type is char *. * (STR + 1) is also a pointer. Its type is char * and it points to Char, which points to "Hi, good morning. the first character 'h', and so on. 3. array name Summary: if an array type array [N] is declared, the array name array has two meanings: First, it represents the entire array, its type is type [N]; second, it is a pointer. the pointer type is type *, and the pointer type is type, that is, the type of the array unit, the Pointer Points to the memory zone, which is the unit 0th of the array. the pointer occupies a separate memory zone. Note that it is different from the memory zone occupied by the array unit 0th. The pointer value cannot be modified.Array ++Is incorrect. In different expressions, array names can play different roles. (1) In the expression sizeof (array), the array name array represents the array itself. Therefore, the sizeof function measures the size of the entire array. (2) In expression * array, array acts as a pointer, so the result of this expression is the value of unit 0th of the array. Sizeof (* array) measures the size of the array unit. (3) expression array + N (where n = 0, 1, 2 ,.....) In, array is a pointer, so the result of array + N is a pointer, its type is type *, it points to type, it points to the array number n. Therefore, sizeof (array + n) measures the pointer size. Iv. definition and use of array pointers: Example 3: int array [10]; INT (* PTR) [10];// Array pointer PTR, pointing to an array int [10] PTR = & array; In the above example, PTR is a pointer and its type is int (*) [10], the Type pointed to is int [10]. We use the first address of the entire array to initialize it. In the statement PTR = & array, array represents the array itself. 5. Result of sizeof operation on pointer and array: sizeof (pointer name) determines whether it is the size of the pointer's own type or the size of the type pointed to by the pointer? The answer is the former. For example: int (* PTR) [10]; In a 32-bit program, there are: sizeof (INT (*) [10]) = 4 sizeof (INT [10]) = 40 sizeof (PTR) = 4 in fact, sizeof (object) measures the size of the object's own type, rather than the size of other types.

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.