对于函数scanf_s();
当出现C6064这个警告的时候,预示着缺少一个整形参量,该参量的功能是表明输入字符的长度(可以使用sizeof,strlen 等函数);
例子如下:
scanf_s("%s", book.title,sizeof(book.title));
完整源代码如下:
(结构体传参的测试)
#include<stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[128]; char auther[40]; float price; struct Date date; char publisher[40]; }; struct Book GetInput(struct Book book) { printf_s("请输入书名:"); scanf_s("%s", book.title,sizeof(book.title)); printf_s("请输入作者:"); scanf_s("%s", book.auther,sizeof(book.auther)); printf_s("请输入售价:"); scanf_s("%f", &book.price); printf_s("请输入出版日期:"); scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day); printf_s("请输入出版社:"); scanf_s("%s", book.publisher,sizeof(book.publisher)); return book; } void printBook(struct Book book) { printf_s("书名 :%s \n", book.title); printf_s("作者 :%s \n", book.auther); printf_s("售价 :%f \n", book.price); printf_s("出版日期 :%d-%d-%d\n", book.date.year, book.date.month,book.date.day); printf_s("出版社 :%s\n", book.publisher); } int main() { struct Book b1={0}, b2={0}; printf_s("请输入第一本书的信息..."); putchar('\n'); b1 = GetInput(b1); printf_s("请输入第二本书的信息..."); b2 = GetInput(b2); printf_s("录入完毕,现在输出:\n"); printf_s("第一本书的信息:\n"); printBook(b1); printf_s("第二本书的信息:\n"); printBook(b2); return 0; }
讯享网
输入使用 scanf() ,是不安全的,因为他在读取字符串的时候不会检查边界,可能会造成内存泄露,在 Visual Studio 中编译会报错,推荐使用 scanf_s() 。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/15729.html