C语言,数组定义,错误

来源:百度知道 编辑:UC知道 时间:2024/06/15 09:25:20
各位大侠好,我做了个练习,不知道错在哪了,请大侠们千万指教,感激不尽!
部分代码:
#include<stdio.h>
#define MAX_TERMS 100
#define MAX_MATRIX 100
typedef struct
{
int row;
int col;
int value;
}Terms;

void main(void)
{
void Transpose(Terms destination, Terms source);
int i;
int j;
Terms source[MAX_TERMS] = {{6,6,8}, {0,0,5}, {0,3,22},
{0,5,-15}, {1,1,11}, {1,2,3},
{2,3,-6}, {4,0,91}, {5,2,28}};
Terms destination[MAX_TERMS];
void Transpose(Terms destination[], Terms source[]);

int sparse_matrix[source[0].row][source[0].col];
int tran_sparse_matrix[destination[0].row][destination[0].col];

这是错误信息(信息都是指最后面的那两个数组):
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
expected constant expression
error C2466: cannot allocate an array of con

你后面2各是二维数组吧。。。一般不能用变量来定义数组的行数和列数的。。
你前面定义Terms destination[MAX_TERMS];是用MAX_TERMS常量来定义大小的。。。所以通过。。后面就不对了。。你后面用的是结构体数组中的一个结构体成员来定大小,这是变量,当然不可以。。。

destination[0].row
source[0].row
是变量!同意1楼。

二楼正解:不能用变量定义数组大小

代码没发完 欢迎在线交流