问题遇到的现象和发生背景
使用Visual Studio 2022,scanf()函数用不了
问题相关代码
#include <iostream>
#include<stdio.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
int data;
struct LNode* next;
}LNode;
int main()
{
void createlistF(LNode * &, int [], int );
void printlist(LNode *);
LNode* C;
int a[maxSize],n;
cout << "Please enter the number of elements:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
createlistF(C, a, n);
printlist(C);
return 0;
}
void createlistF(LNode * &C, int a[], int n)
{
LNode* s;
C = (LNode*)malloc(sizeof(LNode)); //C is the head node of the linked list, it does't store datum
C->next = NULL;
for (int i = 0; i<n; i++)
{
s = (LNode*)malloc(sizeof(LNode));
s->next = C->next;
C->next = s;
s->data = a[i];
}
return;
}
void printlist(LNode* C)
{
C = C->next;
while (C)
{
printf("%d", C->data);
if (C->next)
{
printf(" -> ");
}
}
return;
}
运行结果及报错内容
编译错误,第25行:Severity Code Description Project File Line Suppression State
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. data_structure D:\programs\visual_studio_projects\data_structure\header_insertion_for_singlelinked_list.cpp 25
我的解答思路和尝试过的方法
1)换成其他IDE, 我换的是Dev-C++,在Dev-C++上编译没有问题
2)按照报错内容的提示,把scanf改成scanf_s就没有问题了
我想要达到的结果
请问为什么在Visual Studio上无法使用scanf函数,为什么改成scanf_s以后又可以使用了?