33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int matrix[3][3] = {
{0, 0, 2, 0}
,{3, 0, 0, 1}
,{0, 4, 0, 0}
,{8, 0, 0, 0}
};
#define MAXSIZE 12
typedef struct{
int i,j;
int e;
}Triple;
typedef struct{
Triple data[MAXSIZE + 1];
int mu,nu,tu;
}TSMatrix;
TSMatrix convertTSmatrix(int m[3][3])
{
TSMatrix msmatrix;
msmatrix.mu = sizeof(m) - 1;
msmatrix.nu = sizeof(m[0]) - 1;
Triple triples[MAXSIZE + 1];
int tCount = 0;
int i, j;
for (i = 0; i < msmatrix.mu; i++)
{
for (j = 0; j < msmatrix.nu; j++)
{
Triple triple;
if (m[i][j] != 0)
{
triples[tCount].i = i;
triples[tCount].j = j;
triples[tCount].e = m[i][j];
tCount++;
}
}
}
msmatrix.tu = tCount;
msmatrix.data = triples;//此处有问题,错误提示说指针不能赋值到数组。
return msmatrix;
}
int main()
{
/*
[0, 0, 2, 0]
[3, 0, 0, 1]
[0, 4, 0, 0]
[8, 0, 0, 0]
*/
TSMatrix tsMatrix = convertTSmatrix(matrix);
int a[10];
printf("maxsize=%i", sizeof(a));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int matrix[4][4] = {
{0, 0, 2, 0}
,{3, 0, 0, 1}
,{0, 4, 0, 0}
,{8, 0, 0, 0}
};
#define MAXSIZE 12
typedef struct{
int i,j;
int e;
}Triple;
typedef struct{
Triple data[MAXSIZE + 1];
int mu,nu,tu;
}TSMatrix;
TSMatrix convertTSmatrix(int m[4][4], int row, int col )
{
TSMatrix msmatrix;
msmatrix.mu = row;
msmatrix.nu = col;
Triple triples[MAXSIZE + 1];
int tCount = 0;
int i, j;
for (i = 0; i < msmatrix.mu; i++)
{
for (j = 0; j < msmatrix.nu; j++)
{
if (m[i][j] != 0)
{
triples[tCount].i = i;
triples[tCount].j = j;
triples[tCount].e = m[i][j];
tCount++;
}
}
}
msmatrix.tu = tCount;
memcpy( msmatrix.data, triples, sizeof(Triple)*tCount );
return msmatrix;
}
int main()
{
/*
[0, 0, 2, 0]
[3, 0, 0, 1]
[0, 4, 0, 0]
[8, 0, 0, 0]
*/
TSMatrix tsMatrix = convertTSmatrix(matrix, 4, 4);
int a[10];
printf("maxsize=%i", sizeof(a));
return 0;
}