尝试顺序表初始化报错
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <algorithm>
#define MaxSize 1000
typedef int KeyType;
typedef int DataType;
typedef struct entry {
KeyType key;
DataType data;
}Entry;
typedef struct list {
int n;
Entry D[MaxSize];
}List;
const int length = 10000; //数组大小
const int cnt = 50; //重复执行次数
int a[length + 10];
//排序实现略去
void InitList(List &L)
{
for (int i = 0; i < MaxSize; i++)
{
L.D[i].data = 0;
L.D[i].key = 0;
}
*&L.n = 0; //初始化长度为0写入位置 0xCCCCCCCC 时发生访问冲突。
}
int main()
{
int choice;
List *L = NULL;
InitList(*L);
while (1)
{
Menu();
L->n = length;
srand(time(0));
for (int i = 0; i < L->n; i++)
{
L->D[i].data = rand();
}
scanf_s("%d",&choice);
if (choice >= 1 && choice <= 6)
{
SortOne(L,choice);
}
else if (choice == 0)
{
return 0;
}
else
{
printf("输入有误\n");
}
}
return 0;
}
调试后发生异常
List *L = NULL;
你都没分配空间,怎么赋值呢,当然崩溃了
你这完全不需要定义成指针啊,直接List L;就可以了