Grin_Ryou
2019-04-20 21:39
采纳率: 100%
浏览 967

为什么在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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2019-04-21 01:01
    已采纳

    scanf_s("%s", name);
    ->
    scanf_s("%s", name, 40); //需要缓冲区长度的参数

    点赞 打赏 评论

相关推荐 更多相似问题