C4473结构分配警告 [英] C4473 warning for structure assignment

查看:74
本文介绍了C4473结构分配警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一项作业,并且好奇该警告在编译时是什么以及如何解决.它会生成,但是在我调试时会出现错误屏幕.以下是出现的警告.

I am currently working on an assignment and was curious what this warning is when compiling and how to remedy it. It will build but when I debug it will get an error screen. Below is the warning that comes up.

1> c:\ users \ cesteves \ documents \ c编程\库存\库存\库存.cpp(48):警告C4473:'scanf_s':传递的格式字符串参数不足

1>c:\users\cesteves\documents\c programming\inventory\inventory\inventory.cpp(48): warning C4473: 'scanf_s' : not enough arguments passed for format string

注意:占位符及其参数需要2个可变参数,但提供了1个

note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided

注意:格式字符串要求缺少可变参数2'%s'注意:此参数用作缓冲区大小

note: the missing variadic argument 2 is required by format string '%s' note: this argument is used as a buffer size

#include "stdafx.h"
#include <stdio.h>

void main()
{
    struct date {
        int day;
        int month;
        int year;
    };

    struct details {
        char name[20];
        int price;
        int code;
        int qty;
        struct date mfg;
    };

    struct details item[50];
    int n, i;

    printf("Enter number of items:");
    scanf_s("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Item name: \n");
        scanf_s("%s", item[i].name);
        printf("Item code: \n");
        scanf_s("%d", &item[i].code);
        printf("Quantity: \n");
        scanf_s("%d", &item[i].qty);
        printf("price: \n");
        scanf_s("%d", &item[i].price);
        printf("Manufacturing date(dd-mm-yyyy): \n");
        scanf_s("%d-%d-%d", &item[i].mfg.day, &item[i].mfg.month, &item[i].mfg.year);    
    }

    printf("             *****  INVENTORY ***** \n");
    printf("----------------------------------------------------------------- - \n");
    printf("S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE| MFG.DATE \n");
    printf("----------------------------------------------------------------- - \n");

    for (i = 0; i < n; i++)
        printf("%d     %-15s        %-d          %-5d     %-5d%d / %d / %d \n", i + 1, item[i].name, item[i].code, item[i].qty,item[i].price, item[i].mfg.day, item[i].mfg.month,item[i].mfg.year);
    printf("----------------------------------------------------------------- - \n");
}

推荐答案

您应提供缓冲区的大小.例如,如果您仅读取一个字符,则应该是这样的:

You should provide a size of the buffer. For example, if you reading only one char, it should be like this:

char c;
scanf_s("%c", &c, 1);

请阅读参考

此外, structs 很高兴放在 main()之前.关于 structs的基本用法,我始终牢记示例.

Also, structs are nice to be placed before main(). I always have my example in mind, on the basic usage of structs.

在您的情况下,main的原型应为 int main(void).检查以下内容: int main()vs void main()in C

The prototype of main should be int main(void) in your case. Check this: int main() vs void main() in C

在您的代码中,更改以下内容:

In your code, change this:

scanf_s("%s", item[i].name);

对此:

scanf_s("%s", item[i].name, 20);

因为这个:

struct details {
  char name[20];
  ..  

其余的都做同样的事情.

Do the same for the rest..

这篇关于C4473结构分配警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆