Two-dimensional array and pointer

Source: Internet
Author: User

1. Implement a display function to print the elements in the output array.

Input: pointer to the first element of the array, number of rows and number of columns of the array

Output: None

The flexibility of this function is that we can print the array of any column in any row without having to set the number of rows or columns;

We know that two-dimensional arrays need to determine the number of columns when passing function parameters. This function does not need to fix the number of columns.

# Include <stdio. h>/*** function: display each element in an array ** input: A pointer of the first element and the array's Row and Col ** output: void */void display (int * P, int row, int col) {int I, j; for (I = 0; I <row; I ++) {for (j = 0; j <Col; j ++) {printf ("% 3d", * (p + Col * I + j )); // use * (p + Col * I + J) to represent array [I] [J]} printf ("\ n") ;}} int main () {int array [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }}; display (& array [0] [0], 3,4); // transmits the pointer display (int *) array by & array [0] [0, 3, 4); // You can also convert the array name (pointer to a one-dimensional array) to a (int *) pointer to pass return 0 ;}

The above functions can indeed implement our functions, but there may be such a problem: If the function does not need to print an array, but needs to perform a lot of calculations with arrays, it is bound to reference array [I] [J] multiple times, but for now, we can only use * (p + Col * I + J) array [I] [J], which may be inconvenient and difficult to use. Is there a way to directly reference array [I] [J] In this function?

The following code can be implemented.

Implementation principle:

Dynamically apply for a row pointer space and use a pointer int ** array to point to the first address of the pointer space, in this way, array [I] indicates the I pointer dynamically applied;

Assign values to each pointer to point to the first element of each row in a two-dimensional array.

Remember to release the dynamically applied pointer space after use.

The Code is as follows:

# Include <stdio. h> # include <stdlib. h>/*** function: display each element in an array ** input: A pointer of the first element and the array's Row and Col ** output: void */void display (int * P, int row, int col) {int I, j; int ** array = (INT **) malloc (sizeof (void *) * row); // when applying for a row pointer space, it is used to store the starting position of each row in different Arrays for (I = 0; I <row; I ++) {array [I] = P + I * Col; // array [I] indicates the I pointer dynamically applied, it points to the starting position of row I of the array} for (I = 0; I <row; I ++) {for (j = 0; j <Col; j ++) {// printf ("% 3d", * (p + Col * I + J); // Pass * (p + Col * I + J) array [I] [J] printf ("% 3d", array [I] [J]); // This allows you to easily introduce array elements through indexes} printf ("\ n");} Free (array); // remember to release the dynamically applied memory}

Ii. Dynamic Application of Two-dimensional arrays

We may often encounter the problem of dynamically applying for a two-dimensional array. It is not difficult to apply for a space. We only need to call the malloc function to specify the size of the memory to be applied, the key is how to reference array elements? The simplest method is as follows: apply for an int array [row] [col] as an example.

int *array = (int *)malloc(sizeof(int) * row * col);//array[i][j] = array[i * col + j]//array[i][j] = *(array + i * col + j)

We can see that this is very easy to apply for, but it will be troublesome to use, and we cannot easily reference it through the array [I] [J] We are most used, however, the two methods described in the annotations must be used for reference.

We can see from the previous display function example that we can convert this inconvenient reference into a reference we are used to. This conversion process requires dynamic application of row pointer space, we can call this conversion process as a function, or we can write a function that dynamically applies for two-dimensional arrays, so that we apply for memory to call this function, in this case, you can easily reference it during use.

Void ** malloc_array (INT row, int Col, int size) {void ** array = NULL; int I; int pointsize; pointsize = row * sizeof (void *); // The memory size of the row pointer space to be applied for. array = (void **) malloc (pointsize + size * row * col ); // apply for memory space (row pointer space + array element space) for (I = 0; I <row; I ++) {array [I] = array + pointsize + Col * I; // assign values to the row pointers and point to the start position of each row in the array} return array ;}

Int ** A = (INT **) malloc_array (2, 3, sizeof (INT); // you can directly use a [I] [J]

There will also be some other problems, such as the display method we just wrote, which can output elements in the array without specifying the number of columns in the array, however, the function cannot be used to output the two-dimensional array we dynamically applied. to output the two-dimensional array we dynamically applied, use

void display2(int **p, int row, int col){int i,j;for(i = 0; i < row; i ++){for(j = 0; j <col; j ++){printf("%3d", p[i][j]);}printf("\n");}}

The function is called as follows:

int **a = (int **)malloc_array(2,3,sizeof(int));display2(a,2,3);

 

This also seems to complicate the problem. We need to remember when to call the method. How to use these methods depends on the specific problem.

Note: The memory applied by using the malloc_array method to apply for memory using the malloc code is not sequential, so I don't know why:

Array = (void **) malloc (pointsize + size * row * col); // apply for memory space (row pointer space + array element space)

The Applied memory space is divided into two parts. pointsize bytes are continuous, while size * row * Col bytes are continuous, 8 bytes + 24 bytes (call this method to apply for int array [2] [3]),

What's more amazing is that array + pointsize actually points to the array content space we want to point to. I don't understand it. It seems that I still have a lot to learn!

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.