2015-12-01 75 views
0

這裏是我的代碼:結構問題:初始化將指針整數,未作鑄

#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

我有我的數組列表的麻煩[],特別是當我嘗試輸入電容器的型號時。結構體a,b,c和d的Model元素在輸入到數組List []中時會產生一個「初始化從指針沒有轉換的整數」警告。

我能做些什麼來解決這個問題?

感謝您的幫助。

+0

更改爲:'結構電容列表[] = {a,b,c,d};' –

+0

@PaulGriffiths謝謝,這工作。 –

+0

請複製並粘貼警告(和結果)作爲我們的文本,以便搜索引擎不必解析您的圖像文件,以便爲後代編制索引此問題。請在你的餘生中記住這一點,因爲將文字放入圖像中的人(如你)正在迅速爲互聯網的死亡做出貢獻。 – Sebivor

回答

0

首先,當使用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;