结构问题:初始化从指针生成整数而没有强制转换

时间:2015-12-01 03:09:32

标签: 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 []出现问题,特别是当我尝试输入电容的型号时。结构a,b,c和d的Model元素产生一个&#34;初始化从指针生成整数而没有强制转换&#34;输入数组List []时发出警告。

我该怎么做才能解决这个问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

首先将scanf(&#34;%s&#34;)用于struct时,你不应该使用&amp ;.检查如何正确使用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;等更具动态性。