Grin_Ryou
Grin_Ryou
采纳率100%
2019-04-20 21:39 阅读 942

为什么在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条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2019-04-21 01:01

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

    点赞 1 评论 复制链接分享

相关推荐