学习二维动态数组指针做矩阵运算的方法

2020-01-06 13:36:10王旭

这片文章介绍了如何利用二维动态数组指针做矩阵运算,需要的朋友可以参考下

本文分享了利用二维动态数组指针做矩阵运算的实现代码。

1. 头文件

 

 
  1. // juzhen 2.cpp : Defines the entry point for the console application.  // 
  2.   #include "stdafx.h" 
  3. #include "stdlib.h"  #include "windows.h" 
  4. #define OK 0  #define NG -1 
  5. typedef struct mat  { 
  6. int nRow; /* 行数 */  int nCol; /* 列数 */ 
  7. int* pData; /* 指向矩??体的指? */  }MAT; 

2. 程序代码

 

 
  1. #include "stdafx.h"  #include "Matrix_cal.h" 
  2. /* Entity and initial matrix of the application matrix function */  int MATAlloc(MAT *pMat, int nRow, int nCol) 
  3. {  pMat->pData = (int *) malloc (nRow * nCol * sizeof(int) ); 
  4. if(NULL == pMat->pData)  { 
  5. printf("Memary is error!n");  return NG; 
  6. }  for(int i=0; i<nRow; ++i) 
  7. {  for(int j=0; j<nCol; ++j) 
  8. {  *(pMat->pData + i*nCol + j)=0; 
  9. }  } 
  10. pMat->nRow = nRow;  pMat->nCol = nCol;