C语言中作为函数参数的矩阵
我被困在最基本的问题上。这不是家庭作业,我只是用一种简单的方式来表达这个问题 我需要一个矩阵打印函数,矩阵作为参数与行和列计数信息一起传递。矩阵在堆栈上分配 下面提到的函数内原型MAT_COL是一个编译时定义C语言中作为函数参数的矩阵,c,C,我被困在最基本的问题上。这不是家庭作业,我只是用一种简单的方式来表达这个问题 我需要一个矩阵打印函数,矩阵作为参数与行和列计数信息一起传递。矩阵在堆栈上分配 下面提到的函数内原型MAT_COL是一个编译时定义 void matrix_print(uint8_t (*mat)[MAT_COL], int row, int col) 并将矩阵元素打印为 print mat[i_row][i_col]; 如果我有多个不同大小的矩阵,即“MAT_COL”不再可用,则该方法将不起作用 一个可能的出路是
void matrix_print(uint8_t (*mat)[MAT_COL], int row, int col)
并将矩阵元素打印为
print mat[i_row][i_col];
如果我有多个不同大小的矩阵,即“MAT_COL”不再可用,则该方法将不起作用
一个可能的出路是
void matrix_print(uint8_t *in_mat, int row, int col)
{
uint8_t (*mat)[col] = in_mat; // typecast "col" is an arg
// access them as eariler
print mat[i_row][i_col];
}
这种方法有什么问题吗?在C中,这个问题的标准解决方案是什么?我想你可以找一个结构来帮助你处理这个类型。 如果类型与您的设计相关,则应使其明确 以下是Ansi-C版本,因为您将C/C++作为标记编写。 在C++中,你的矩阵是一个类。
typedef struct
{
int * data;
int IsAllocated;
unsigned row_max;
unsigned col_max;
} Matrix;
void Matrix_construct(Matrix * m, int cols, int rows);
void Matrix_destruct(Matrix * m, int cols, int rows);
static int Private_GetElement(Matrix * m, unsigned col, unsigned row, int * element);
void Matrix_print(Matrix * m);
void Matrix_construct(Matrix * m, int cols, int rows)
{
m->col_max = cols;
m->row_max = rows;
m->data = (int*) malloc(sizeof(int) * cols * rows);
m->IsAllocated = 1;
}
void Matrix_destruct(Matrix * m, int cols, int rows)
{
m->col_max = 0;
m->row_max = 0;
if(m->IsAllocated)
{
free(m->data);
m->IsAllocated = 0;
}
}
static int Private_GetElement(Matrix * m, unsigned col, unsigned row, int * element)
{
int e = 0;
if(m && element && col < m->col_max && row < m->row_max && m->IsAllocated)
{
*element = m->data[col + row * m->col_max];
}
else
{
e |= 1;
}
return e;
}
void Matrix_print(Matrix * m)
{
unsigned col, row;
int element;
for( col = 0; col < m->col_max; ++col)
{
for( row = 0; row < m->row_max; ++row)
{
if(!Private_GetElement(m, col, row, &element))
{
printf("%i", element);
}
}
printf("\n");
}
}
typedef结构
{
int*数据;
int未分配;
无符号行_max;
未签名的colu_max;
}基质;
空矩阵_构造(矩阵*m,整数列,整数行);
无效矩阵_析构函数(矩阵*m,整数列,整数行);
静态int Private_GetElement(矩阵*m,无符号列,无符号行,int*元素);
空矩阵_打印(矩阵*m);
空矩阵_构造(矩阵*m,整数列,整数行)
{
m->col_max=cols;
m->row_max=行;
m->data=(int*)malloc(sizeof(int)*cols*行);
m->IsAllocated=1;
}
无效矩阵_析构函数(矩阵*m,整数列,整数行)
{
m->col_max=0;
m->row_max=0;
如果(m->已分配)
{
免费(m->数据);
m->IsAllocated=0;
}
}
静态int Private_GetElement(矩阵*m,无符号列,无符号行,int*元素)
{
int e=0;
如果(m&&element&&colcol\u max&&rowrow\u max&&m->已分配)
{
*元素=m->数据[col+row*m->col_max];
}
其他的
{
e |=1;
}
返回e;
}
空矩阵_打印(矩阵*m)
{
未签名列,行;
int元素;
用于(col=0;colcol_max;++col)
{
对于(行=0;行行_max;++行)
{
if(!Private_GetElement(m、col、row和element))
{
printf(“%i”,元素);
}
}
printf(“\n”);
}
}
如果在Matrix\u-construct
中分配不同的大小,打印仍会工作,因为Matrix\u-print
使用存储在结构中的最大值
在上面的示例中,使用不同的大小会导致结构的改变,Matrix\u print
仍应按预期工作
编辑:
编辑该示例是为了显示变量大小的动态分配是什么样子。我将您的类型转换从
int
更正为uint8\t
,因此下面的方法确实有效
void matrix_print(uint8_t *in_mat, int row, int col)
{
uint8_t (*mat)[col] = (uint8_t (*)[col])in_mat;
// access them like mat[y][x];
}
但是,如果您试图在可用的地方避免类型转换(这可能是好的做法,也可能不是好的做法),您可以使用如下偏移实现它
void matrix_print(uint8_t *in_mat, int row, int col)
{
// access them like mat[x + y*col];
}
C99支持以下功能声明方式:
void matrix_print(int row, int col, uint8_t in_mat[row][col])
我在gcc C11/C99中使用以下链接对此问题提出了“最终”解决方案:
//使用gcc编译--std=c11 program.c
#包括
#包括
#定义MV(数组,n列,i,j)数组[i*n列+j]
#定义MX9
#定义我的14
无效输入_矩阵(int行、int列、双矩阵[行][列]);
无效打印矩阵(整数行、整数列、双矩阵[行][列]);
int main()
{
int i=MX,j=MY;
printf(“使用函数fn(int w,int k,双矩阵[w][k])(在C99和C11中)生成输入值和打印矩阵”);
双矩阵1[i][j];
输入_矩阵(MX,MY,matrix1);
printf(“矩阵静态\n”);
打印矩阵(MX、MY、matrix1);
双**矩阵2;
matrix2=malloc(MX*sizeof(double*);
matrix2[0]=(双精度*)malloc(MX*MY*sizeof(双精度));
对于(i=1;i 对于(i i=0),你尝试了谷歌吗?BTW如果你在C中编码,为什么你要加标签C++?我做了谷歌,但是没有找到任何STASISFIX答案。我在C中工作,对于GARC平台,用GCC交叉编译器。你的参数不应该是代码> uint 8*t**i*Mata?不需要强制转换,你可以通过<代码>元素访问[Mat] [RO] [COL]
当指针指向uint8_t
时,为什么要对int
进行强制转换?@RakibulHasan,OP的数据集不是指针数组。OP声明他想要动态大小的矩阵。为什么引入强制转换并失去类型安全性?我只是在第一个代码段中修复了OP的版本(无论如何都会发出类型不匹配警告)。
//compile with gcc --std=c11 program.c
#include <stdio.h>
#include <stdlib.h>
#define MV(array, ncolumns, i, j) array[i * ncolumns + j]
#define MX 9
#define MY 14
void input_matrix(int row, int column, double matrix[row][column]);
void print_matrix(int row, int column, double matrix[row][column]);
int main()
{
int i=MX, j=MY;
printf("Generate input values and print matrices with functions fn(int w, int k, double matrix[w][k]) (in C99 and C11)\n");
double matrix1[i][j];
input_matrix(MX,MY,matrix1);
printf("matrix static\n");
print_matrix(MX,MY,matrix1);
double **matrix2;
matrix2=malloc(MX*sizeof(double*));
matrix2[0] = (double *)malloc(MX*MY*sizeof(double));
for(i = 1; i < MX; i++)
matrix2[i] = matrix2[0]+i*MY;
input_matrix(MX,MY,(double (*)[])(*matrix2));
printf("matrix two times allocated one for pointers, the second for data (double (*)[])(m[0])\n");
print_matrix(MX,MY,(double (*)[])(matrix2[0]));
free(*matrix2);
free(matrix2);
double *matrix3;
matrix3=malloc(MX*MY*sizeof(double));
input_matrix(MX,MY,(double (*)[])matrix3);
printf("matrix alocated as twodimensional array\n");
print_matrix(MX,MY,(double (*)[])matrix3);
free(matrix3);
j=MY;
double (*matrix4)[j];
matrix4 = (double (*)[])malloc(MX * sizeof(*matrix4));
input_matrix(MX,MY,matrix4);
printf("matrix alocated as an array of pointers to arrays m = (double (*)[])malloc(MX * sizeof(*m))\n");
print_matrix(MX,MY,matrix4);
free(matrix4);
printf("\nThe End!\n");
return 0;
}
void input_matrix(int row, int column, double matrix[row][column]){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
matrix[i][j]=i+1;
}
}
void print_matrix(int row, int column, double matrix[row][column]){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
printf("%.2lf ", matrix[i][j]);
printf("\n");
}
}