简体   繁体   中英

Structure Issue: initialization makes integer from pointer without a cast

Here is my code:

#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;

}

Output:

Warning

Result

I am having trouble with my array List[], specifically when I try to input the model number of my capacitors. The Model element of structures a, b, c, and d produces a "initialization makes integer from pointer without a cast" warning when entered into the array List[].

What can I do to fix this?

Thanks for the help.

First when using scanf("%s") into struct you shouldn't use &. Check how to use scanf properly with strings.

Then the line with struct Capacitor List[] I don't know what you want to do with it but isn't it easier just to create array of pointers on those already created structures like this:

#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;

}

This is just a quick example, it should be more dynamic than actually using List[0] = a; etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM