加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – scanf_s抛出异常

发布时间:2020-12-14 05:46:09 所属栏目:Windows 来源:网络整理
导读:为什么以下代码在输入要放入结构的数字后到达第二个scanf_s时会引发异常. 这绝不代表完整的链表实现. 输入值时,不确定如何进入下一个scanf_s?有任何想法吗? 编辑:使用建议的解决方案更新了代码,但在第一次scanf_s后仍然获得AccessViolationException 码:
为什么以下代码在输入要放入结构的数字后到达第二个scanf_s时会引发异常.

这绝不代表完整的链表实现.

输入值时,不确定如何进入下一个scanf_s?有任何想法吗?

编辑:使用建议的解决方案更新了代码,但在第一次scanf_s后仍然获得AccessViolationException

码:

struct node
{
    char name[20];
    int age;
    float height;
    node *nxt;
};

int FillInLinkedList(node* temp)
{

int result;
temp = new node;

printf("Please enter name of the person");
result = scanf_s("%s",temp->name);

printf("Please enter persons age");
result = scanf_s("%d",&temp->age); // Exception here...

printf("Please enter persons height");
result = scanf_s("%f",&temp->height);

temp->nxt = NULL;
if (result >0)
    return  1;
 else return 0;
}

// calling code

int main(array<System::String ^> ^args)
{
  node temp;

  FillInLinkedList(&temp);

...

解决方法

你需要

result = scanf_s("%d",&temp->age);

result = scanf_s("%f",&temp->height);

原因是sscanf(和朋友)需要一个指向输出变量的指针,因此它可以将结果存储在那里.

顺便说一句,你的函数参数temp也有类似的问题.由于您正在更改指针(而不仅仅是指向它的内容),因此您需要传递一个双指针,以便在您的函数外部显示更改:

int FillInLinkedList(node** temp)

当然,你必须在函数内部进行必要的更改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读