运行环境 codeblock
运行过程中,输入0-10或者为负数的时候,子函数与主函数输出没有问题,然后输入11及其以上的数字,子函数正确,主函数输出为-1.
最开始我以为是内存空间溢出的问题,把MAX改小后依然存在问题。
然后我把结构体定义int n提到前面,输出就没有问题了。
下面是代码:
#include <stdio.h>
#define MAX 10
typedef struct HashNode *HashTable;
struct HashNode
{
char data[MAX][MAX];
int t[MAX];
int n;
};
HashTable CreatTable(int Size); //创建一个散列表, data为-1代表没有元素
int main(void)
{
HashTable h;
int x;
scanf("%d",&x);
h = CreatTable(x);
printf("out n is %d\n",h->n);
}
HashTable CreatTable(int Size) //创建一个散列表, data为-1代表没有元素
{
HashTable h;
int i;
h = (HashTable)malloc(sizeof(struct HashNode));
if(h == NULL)
{
printf("malloc error\n");
exit(-1);
}
h->n = Size;
printf("in n is %d\n",h->n);
for(i = 0; i < Size; i++)
h->t[i] = -1;
return h;
}
错误运行案列:
int n提前后
#include <stdio.h>
#define MAX 10
typedef struct HashNode *HashTable;
struct HashNode
{
int n;
char data[MAX][MAX];
int t[MAX];
};
HashTable CreatTable(int Size); //创建一个散列表, data为-1代表没有元素
运行如下
求解答,感激不尽。