新手求助,动态数组申请
typedef struct Matrix{
int height;
int width;
float*element;
}Matrix;
int main(){.......
;
cout<<"输入A矩阵的行数"
<<endl;
cin>>A.height;
cout<<"输入A矩阵的宽度"
<<endl;
cin>>A.width;
if((A.element=new float[A.height*A.width])==NULL){
cout<<"不能分配"<<endl;
return(1);
}
init(&A);
B.height=A.width;
cout<<"输入B矩阵的宽度"
<<endl;
cin>>B.width;
if((B.element=new float[B.height*B.width])==NULL){
cout<<"不能分配"<<endl;
return(1);
}
//在堆中开辟一个4×5的二维int数组
#include <stdio.h>
#include <malloc.h>
int **p;
int i,j;
void main() {
p=(int **)malloc(4*sizeof(int *));
if (NULL==p) return;
for (i=0;i<4;i++) {
p[i]=(int *)malloc(5*sizeof(int));
if (NULL==p[i]) return;
}
for (i=0;i<4;i++) {
for (j=0;j<5;j++) {
p[i][j]=i*5+j;
}
}
for (i=0;i<4;i++) {
for (j=0;j<5;j++) {
printf(" %2d",p[i][j]);
}
printf("\n");
}
for (i=0;i<4;i++) {
free(p[i]);
}
free(p);
}
// 0 1 2 3 4
// 5 6 7 8 9
// 10 11 12 13 14
// 15 16 17 18 19
# include <iostream>
using namespace std;
struct Matrix {
int height;
int width;
float * element;
};
int main()
{
Matrix A, B;
cout << "input A's height" << endl;
cin >> A.height;
cout << "input A's width" << endl;
cin >> A.width;
A.element = new float[A.height * A.width];
// init(&A);
B.height = A.width;
cout << "input B's width" << endl;
cin >> B.width;
B.element = new float[B.height * B.width];
return 0;
}