(In-depth understanding of the C pointer) learning notes and summary chapter 4 pointers and Arrays

Source: Internet
Author: User

(In-depth understanding of the C pointer) learning notes and summary chapter 4 pointers and Arrays
What is an array:
Arrays and pointers are different in my understanding. Because of the similarities, arrays and pointers are essentially the same in some documents. Because of the differences, there are also some documents that the array and pointer are different.
Similarities:
Both the array name and pointer name represent an address.
For example, int num [10]; num is the array name. The function opens up a space to store ten integer types, and num is their first address.
Int * p;
P = (int *) malloc (10 * sizeof (int); similarly, p points to the first address.
The difference is that the space position in num [10] is in the stack, and the space that x p points to is in the heap.
P can point to another address (that is, p can perform pointer operations. Is a variable) But num cannot be used. It is a constant.
One-dimensional array and pointer array:
Int num [5] = {1, 2, 3, 4, 5 };
When referencing: printf ("% d", num [I]);
Int * num2 [5] = {& n1, & n2, & n3, & n4, & n5 };
When referencing: printf ("% d", * num [I]);
Two-dimensional array:
Int num [I] [j];


You can use malloc to create an array:
Int * pv = (int *) malloc (5 * sizeof (int ));
For (I = 0; I <5; I ++)
P [I] = I + 1; // * (p + I) = I + 1;
One-dimensional array of pointers:
Int * arr [5];
Int I;
For (I = 0; I <5; I ++)
{
Arr [I] = (int *) malloc (sizeof (int ));
* Arr [I] = I;
}
Or:
* (Arr + I) = (int *) malloc (sizeof (int ));
** (Arr + I) = I;
Second analysis: (arr + I) represents the address of the array's I-th element. We need to modify the content in this address, so we use × (arr + I) and the content of arr + I is a pointer pointing to a memory. Therefore, this solution is used. The returned result is the location of the allocated memory.
Transfer of two-dimensional arrays:
Take 2.c as an example. Although it is a two-dimensional array, it is used in a function as a one-dimensional array. Because the Declaration is a one-dimensional array. Therefore, the content in the array cannot be used in the arr [I] [j] Method in the subfunction. Only
Arr + offset and then describe: * (arr + (I * cols) + j)
You can also dynamically allocate two-dimensional arrays:
Int rows2;
Int columns = 5;
Int ** matrix = (int **) malloc (rows * sizeof (int *); // The type is a two-dimensional array. Each element in it is an integer pointer.
For (I = 0; I {
Matrix [I] = (int *) malloc (colums * sizeof (int); // each element points to an integer one-dimensional array.
}

This method is similar to the string pointer representation.

Sample Code:

1 // pointer array:
# Include
Int main (void)
{
Int * num1 [3];
Int num2 [3] = {1, 2, 3 };
Int I;
For (I = 0; I <3; I ++)
Num1 [I] = & num2 [I];
For (I = 0; I <3; I ++)
Printf ("% 4d", * num1 [I]);
Putchar ('\ n ');
Return 0;
}

2. Two-dimensional array representation:
# Include
Void arr_initial (int * arr, int rows, int cols)
{
Int I, j;
Int num = 1;
For (I = 0; I For (j = 0; j {
* (Arr + (I * cols) + j) = num ++;
}
}
Void arr_print (int * arr, int rows, int cols)
{
Int I, j;
Int num = 1;
For (I = 0; I {For (j = 0; j {
Printf ("% 4d", * (arr + (I * cols) + j ));
}
Putchar ('\ n ');
}
}


Int main (void)
{
Int num1 [5] [6];
Int num2 [7] [8];
Arr_initial (& num1 [0] [0], 5, 6 );
Arr_print (& num1 [0] [0], 5, 6 );
Arr_initial (& num2 [0] [0], 7,8 );
Arr_print (& num2 [0] [0], 7,8 );
Return 0;
}


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.