用的是Visual Studio Community 2017版本,创建了一个win32程序项目.cpp文件,bug提示程序里两行关于scanf语句的用法错误,原因不明。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 100
#define LIST_INIT_SIZE 4
#define OVERFLOW -1;
#define OK 1;
typedef int DataType;
typedef int Status;
typedef struct//定义顺序表
{
DataType *elem;
int length;
int listsize;
}SeqList;
Status InitSeqList(SeqList **pL)//初始化顺序表
{
SeqList *L;//将参数SeqList *L改为SeqList **pL,函数内部并多加一行用来定义SeqList类型的指针L。
L = (SeqList*)malloc((LIST_INIT_SIZE) * sizeof(SeqList));//将L->elem改为L,将DataType改为SeqList。
if (!L->elem)
exit(-1);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
(*pL) = L;
return OK;
}
int main()
{
SeqList *List;
InitSeqList(&List);
int temp = 0;
printf("请输入即将创建的顺序表的长度:\n");
scanf("%d", &temp);
List->length = temp;
int i = 0;
printf("请输入顺序表的数据元素:\n");
for (; i < List->length; i++)
{
scanf("%d", &temp);* List->elem[i] = temp;
//elem=elem+sizeof(DataType);
}
printf("顺序表的存储内容:\n");
for (; i < List->length; i++)
{
printf("%d", (List->elem[i]));
//elem=elem+sizeof(DataType);
}
return 0;
}
bug提示信息为:
1>------ 已启动生成: 项目: Project1, 配置: Debug Win32 ------
1>源.cpp
1>c:userslenovodocumentsvisual studio 2017projectsproject1project1源.cpp(45): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>c:program files (x86)windows kits10include10.0.15063.0ucrtstdio.h(1272): note: 参见“scanf”的声明
1>c:userslenovodocumentsvisual studio 2017projectsproject1project1源.cpp(51): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>c:program files (x86)windows kits10include10.0.15063.0ucrtstdio.h(1272): note: 参见“scanf”的声明
1>已完成生成项目“Project1.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
bug截图:
自己有尝试着修改scanf括号里的语句用法,未果……
不是说了scanf不安全让你用scanf_s替代么?
主要是scanf不会检查边界,比如
char in[2];
scanf("%s", in);//abcdef
这就越界了,然后崩了,类似的还有sprintf
不过scanf_s不是跨平台的,所以你保证不会越界,然后加_CRT_SECURE_NO_WARNINGS这个宏也可以