为什么在C语言使用数组时会发生访问冲突?
问题描述:
我想请问一下这里为什么会在scanf_s("%s", name);处抛出异常,调整了堆栈保留大小也不行,谢谢
#include <stdio.h>
#include <string.h>
#define DENSITY 62.4
int main()
{
float weight, volume ;
int size, letters;
char name[40];
printf("Hi! What's your first name.\n");
scanf_s("%s", name);
printf("%s, what's your weight in pounds?\n", name);
scanf_s("%f", &weight);
size = sizeof name;
letters = strlen(name);
volume = weight / DENSITY;
printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
printf("Also, your first name has %d letters,\n", letters);
printf("and we have %d bytes to store it.\n", size);
getchar();
return 0;
}
答
scanf_s("%s", name);
->
scanf_s("%s", name, 40); //需要缓冲区长度的参数