c - 结构问题: initialization makes integer from pointer without a cast

标签 c pointers data-structures

这是我的代码:

#include <stdio.h>
#include <string.h>
#define Length 20

    struct Capacitor
    {
        char Model[Length];
        int Capacitance;
        float Voltage;
        float Cost;
    };

void displayCapacitorInfo(struct Capacitor List[])
{
    int i;
    for (i = 0; i < 4; i++)
    {
        printf("Capacitor %s:\n\n", List[i].Model);
        printf("   *Capacitance: %d uF\n", List[i].Capacitance);
        printf("   *Voltage: %f V\n", List[i].Voltage);
        printf("   *Cost: $%f\n", List[i].Cost);
        printf("\n");
    }
}

int main()
{
    float Cost, Voltage;
    int Capacitance;
    char Model[Length];

    struct Capacitor a;
    struct Capacitor b;
    struct Capacitor c;
    struct Capacitor d;

    strcpy(a.Model, "11-123U");
    a.Capacitance = 100;
    a.Voltage = 25;
    a.Cost = 6.00;

    strcpy(b.Model, "65T91a");
    b.Capacitance = 22000;
    b.Voltage = 20;
    b.Cost = 25.00;

    printf("Model number of 1st capacitor: %s\n", a.Model);
    printf("Voltage of 2nd capacitor: %f V\n", b.Voltage);

    printf("\n");

    printf("Model number of 3rd capacitor:");
    scanf("%s", &c.Model);
    printf("\n");

    printf("Capacitance of 3rd capacitor:");
    scanf("%d", &c.Capacitance);
    printf("\n");

    printf("Voltage of 3rd capacitor:");
    scanf("%f", &c.Voltage);
    printf("\n");

    printf("Cost of 3rd capacitor:");
    scanf("%f", &c.Cost);
    printf("\n\n");

    printf("Model number of 4th capacitor:");
    scanf("%s", &d.Model);
    printf("\n");

    printf("Capacitance of 4th capacitor:");
    scanf("%d", &d.Capacitance);
    printf("\n");

    printf("Voltage of 4th capacitor:");
    scanf("%f", &d.Voltage);
    printf("\n");

    printf("Cost of 4th capacitor:");
    scanf("%f", &d.Cost);
    printf("\n\n");

    struct Capacitor List[] = { {a.Model, a.Capacitance, a.Voltage, a.Cost}, {b.Model, b.Capacitance, b.Voltage, b.Cost}, {c.Model, c.Capacitance, c.Voltage, c.Cost}, {d.Model, d.Capacitance, d.Voltage, d.Cost} };

    displayCapacitorInfo(List);

    return 0;

}

输出:

Warning

Result

我的数组 List[] 遇到问题,特别是当我尝试输入电容器的型号时。当输入到数组 List[] 时,结构 a、b、c 和 d 的 Model 元素会产生“初始化从指针生成整数而不进行转换”警告。

我可以做什么来解决这个问题?

感谢您的帮助。

最佳答案

首先,在结构体中使用 scanf("%s") 时,不应使用 &。检查如何正确使用 scanf 和字符串。

然后是 struct Capacitor List[] 行,我不知道你想用它做什么,但是在已经创建的结构上创建指针数组不是更容易吗? :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Length 20

struct Capacitor
{
    char Model[Length];
    int Capacitance;
    float Voltage;
    float Cost;
};

void displayCapacitorInfo(struct Capacitor List[])
{
    int i;
    for (i = 0; i < 4; i++)
    {
        printf("Capacitor %s:\n\n", List[i].Model);
        printf("   *Capacitance: %d uF\n", List[i].Capacitance);
        printf("   *Voltage: %f V\n", List[i].Voltage);
        printf("   *Cost: $%f\n", List[i].Cost);
        printf("\n");
    }
}

int main()
{
    float Cost, Voltage;
    int Capacitance;
    char Model[Length];

    struct Capacitor a;
    struct Capacitor b;
    struct Capacitor c;
    struct Capacitor d;

    strcpy(a.Model, "11-123U");
    a.Capacitance = 100;
    a.Voltage = 25;
    a.Cost = 6.00;

    strcpy(b.Model, "65T91a");
    b.Capacitance = 22000;
    b.Voltage = 20;
    b.Cost = 25.00;

    printf("Model number of 1st capacitor: %s\n", a.Model);
    printf("Voltage of 2nd capacitor: %f V\n", b.Voltage);

    printf("\n");

    printf("Model number of 3rd capacitor:");
    scanf("%s", c.Model);
    printf("\n");

    printf("Capacitance of 3rd capacitor:");
    scanf("%d", &c.Capacitance);
    printf("\n");

    printf("Voltage of 3rd capacitor:");
    scanf("%f", &c.Voltage);
    printf("\n");

    printf("Cost of 3rd capacitor:");
    scanf("%f", &c.Cost);
    printf("\n\n");

    printf("Model number of 4th capacitor:");
    scanf("%s", d.Model);
    printf("\n");

    printf("Capacitance of 4th capacitor:");
    scanf("%d", &d.Capacitance);
    printf("\n");

    printf("Voltage of 4th capacitor:");
    scanf("%f", &d.Voltage);
    printf("\n");

    printf("Cost of 4th capacitor:");
    scanf("%f", &d.Cost);
    printf("\n\n");

    //struct Capacitor List[] = { {a->Model, a.Capacitance, a.Voltage, a.Cost}, {*b.Model, b.Capacitance, b.Voltage, b.Cost}, {*c.Model, c.Capacitance, c.Voltage, c.Cost}, {*d.Model, d.Capacitance, d.Voltage, d.Cost} };
    struct Capacitor *List;
    List = malloc(4*sizeof(struct Capacitor*));
    List[0] = a;
    List[1] = b;
    List[2] = c;
    List[3] = d;
    displayCapacitorInfo(List);

    return 0;

}

这只是一个简单的示例,它应该比实际使用 List[0] = a; 等更加动态。

关于c - 结构问题: initialization makes integer from pointer without a cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34011913/

相关文章:

c - 不是 JPEG 文件 : Starts with 0x00 0x00

algorithm - 哈希表和桶数组

Java或php树结构问题

algorithm - 算法的递归方程

c - 如何在 Window 7 后台运行 C 窗口程序

c - 如何将语言绑定(bind)到 C 库

c++ - 当使用 'new int[n]' 声明一个新数组时,为什么它没有出现在本地列表中?

c - 保护 scanf 不使用定义读取太多字符?

c++ - 在 MFC 中添加加速器(快捷方式) - 如何?

c - 使用指针从 C 中的函数调用数组元素