Some scattered and fragmentary knowledge _int ** pointer and a two-dimensional array of problems in C ++

table of Contents

1, int ** Pointer with two-dimensional array problem

1, int ** Pointer with two-dimensional array problem

01) define a two-dimensional array of methods:

int Matrix [ROWS] [COLUMNS];   // define a two-dimensional array, wherein the constant ROWS and COLUMNS

02) Functions added the following statement:

void printMatrix(int ** numbers,int rows,int columns);

03) If you call directly using the following method, it is wrong;

; printMatrix (the Matrix, ROWS, the COLUMNS)   // direct call so wrong

Because the matrix is int (*) [COLUMNS] type , but the function is needed int ** printMatrix type , both of which obviously do not match.

int ** from speaking is a pointer to type integer pointer, then if you want to use it to represent a matrix need to how to do it? Because it is a pointer to the elements, if if its every element represents a row of the matrix, then it can be used to represent a matrix of. Codes are as follows:

 1 //生成矩阵  
 2 int ** generateMatrix(int rows,int columns)  
 3 {  
 4     int **numbers=new int*[rows];  
 5     for(int i=0;i<rows;i++){  
 6         numbers[i]=new int[columns];  
 7         for(int j=0;j<columns;j++)  
 8                 numbers[i][j]=i*columns+j;  
 9     }  
10     return numbers;   
11 }
Int ** represented by a matrix

The int * as a whole. It represents creates an array of size rows, each element represents a pointer to the array. Memory layout as follows:

 

 

Here numbers is a pointer to a pointer can be used to represent numbers with the key matrix is ​​that the new keyword allocated memory is continuous, so that number [i] can be calculated from the address of the address numbers, since the pointer variable occupies 4 bytes of memory areas (32-bit machines). If you do not allocate memory using the above way, numbers really just a pointer to a pointer

04) the proper use of printMatrix (matrix, ROWS, COLUMNS) test code:

 1 #include <stdlib.h>  
 2 #include <stdio.h>  
 3 #include <iostream>  
 4 //打印矩阵  
 5 void printMatrix(int ** numbers,int rows,int columns){  
 6     for(int i=0;i<rows;i++)  
 7     {  
 8             for(int j=0;j<columns;j++)  
 9                     std::cout<<numbers[i][j]<<" ";  
10         std::cout<<std::endl;  
11     }  
12 }  
13   
14 //生成矩阵  
15 int ** generateMatrix(int rows,int columns)  
16 {  
17     int **numbers=new int*[rows];  
18     for(int i=0;i<rows;i++){  
19         numbers[i]=new int[columns];  
20         for(int j=0;j<columns;j++)  
21                 numbers[i][j]=i*columns+j;  
22     }  
23     return numbers;   
24 }  
25 int main(){  
26     int **numbers=generateMatrix(4,5);  
27     printMatrix(numbers,4,5);  
28     //释放内存  
29     for(int i=0;i<4;i++)  
30             delete [] numbers[i];  
31     delete numbers;  
32     return 0;
View Code

 Reference blog:

https://www.cnblogs.com/lpxblog/tag/C%2B%2B%E9%9D%A2%E8%AF%95%E9%A2%98/

Guess you like

Origin www.cnblogs.com/YiYA-blog/p/11456085.html