我正在尝试创建一个生成随机矩阵的结构,并且在“矩阵”之前得到“错误:预期的â€,â,â,â,âm或â€编译时如何才能有效地工作?
我猜错误通常是由错别字引起的,但我没有看到任何错误。
我对C很新,所以指针和malloc对我来说都很陌生。我非常感谢你的帮助。
/* It's called RandomMatrixMaker.c */
#include <stdio.h>
#include <stdlib.h>
typdef struct {
char* name;
int MID;
int MRows;
int MCols;
long[][]* MSpace;
} matrix;
matrix makeRIDMatrix(char* name, int MID, int MRows, int MCols) {
matrix m;
static int i, j, r;
m.name = name;
m.MID = MID;
m.MRows = MRows;
m.MCols = MCols;
for (i=0; i<m.MRows; i++) {
for (j=0; i<m.MCols; j++) {
r = random(101);
*(m.MSpace[i][j]) = r;
}
}
return m;
}
int main(void) {
makeRIDMatrix("test", 1, 10, 10);
return 0;
}
答案 0 :(得分:4)
确实存在拼写错误。你拼错了typedef
:
typdef struct {
应该是:
typedef struct {
编辑:
此外,没有理由在这里使用static
:
static int i, j, r;
你可以摆脱static
修饰符。
int i, j, r;
答案 1 :(得分:2)
正如另一张海报所提到的那样,有一个拼写错误,但即使纠正了,由于matrix.MSpace的定义,它也无法编译。
让我们从makeRIDMatrix()开始吧。您已声明了“matrix”类型的自动(堆栈)变量。在函数结束时,您将返回该对象。虽然这是允许的,但这是不可取的。如果结构很大,则会不必要地复制大量数据。最好将指针传递给makeRIDMatrix(),并让makeRIDMatrix()填入内容。
内循环中的测试是针对i的,但应该针对j。
接下来,让我们看一下“矩阵”的定义。 “MSpace”的定义是一团糟,甚至不会编译。即使它确实如此,因为您还没有定义行的长度,编译器将无法计算数组中任何给定项的偏移量。你想要一个没有给出行长度的二维数组,但你不能用C语言做到这一点。你可以用其他语言,但不能用C语言。
我可以指出更多,但我会错过真正的观点。真正的意思是:
C不是Java。
(它也不是JavaScript,PHP,Python,Ruby等解释语言之一。)
你没有得到动态扩展的数组;你没有自动分配内存;你没有得到未引用内存的垃圾收集。
你需要的是更像这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char* name;
int MID;
unsigned int MRows;
unsigned int MCols;
long *MSpace;
} matrix;
void makeRIDMatrix(matrix *pmx, char* name, int MID,
unsigned int MRows, unsigned int MCols) {
int i, j;
long *MSpace = malloc(sizeof(*MSpace)*MRows*MCols);
if (MSpace == NULL) {
return;
}
pmx->name = name;
pmx->MID = MID;
pmx->MRows = MRows;
pmx->MCols = MCols;
pmx->MSpace = MSpace;
srandom((unsigned int)time(NULL));
for (i=0; i<MRows; i++) {
for (j=0; i<MCols; j++) {
long int r = random() % 101L;
*(MSpace++) = r;
}
}
}
inline long * item_addr(const matrix *pmx,
unsigned int row, unsigned int col) {
if (pmx == NULL || pmx->MSpace == NULL
|| row >= pmx->MRows || col >= pmx->MCols) {
return NULL;
}
return &(pmx->MSpace[row * pmx->MCols + col]);
}
long get_item(const matrix *pmx, unsigned int row, unsigned int col) {
long *addr = item_addr(pmx, row, col);
return addr == NULL ? 0L : *addr;
}
void set_item(matrix *pmx,
unsigned int row, unsigned int col,
long val) {
long *addr = item_addr(pmx, row, col);
if (addr != NULL) {
*addr = val;
}
}
int main(void) {
matrix m;
makeRIDMatrix(&m, "test", 1, 10, 10);
return 0;
}
请注意这里的一些事情。首先,为了提高效率,我将数组填充为一维数据。为安全起见,所有后续的get / set数组项都应该通过getter / setter函数完成。
其次,一个隐藏的讨厌:makeRIDMatrix()已经使用malloc()来分配内存 - 但它将成为调用函数(或其后继函数)的工作,以便在它完成时释放()分配的指针。
第三,我已经将rows / cols变量更改为unsigned int - 确定具有负索引的数组没什么意义!
第四:小错误检查。例如,makeRIDMatrix()既不知道也不关心参数值是否合理(例如,不检查矩阵指针是否为NULL)。这是学生的练习。
第五,我已经修改了你的随机数字用法 - 经过时尚。学生的另一项练习:为什么我这样做不是好习惯?
然而 - 所有这一切都没有实际意义。你需要给自己一本好的C教科书或一本好的在线课程,并通过这些例子来完成。你在这里给出的代码显示你此刻正在超过你的体重,你需要在进入那个环之前开发更多的C肌肉!
答案 2 :(得分:1)
关于“可变大小数组”的问题,您可以使用以下内容:
/* can stick this into your struct, this is just an example */
size_t rows, cols;
long **matrix;
/* set the values of rows, cols */
/* create the "array" of rows (array of pointers to longs) */
matrix = (long**)malloc(rows * sizeof(long*));
/* create the array of columns (array of longs at each row) */
for (i = 0; i < rows; i++)
matrix[i] = (long*)malloc(cols * sizeof(long));
/* ... */
/* free the memory at the end */
for (i = 0; i < rows; i++)
free(matrix[i]);
free(matrix);
然后你可以访问动态分配的矩阵,类似于任何其他数组。
即。将第一行(第0行)和第四列(第3列)的元素设置为5:
matrix[0][3] = 5;