如何将float数组数组复制到struct中的float数组数组中

时间:2014-05-31 18:01:47

标签: c arrays floating-point double

我尝试过使用strcpy(它说它需要字符串的char类型)并且不起作用

以下是代码:

void AddNewTemp(void) {


int i=0,x,k=1,z=0,f=0;
float Sum=0.00;
float temperatures[8];

do{
fflush(stdin);
printf("Temperatures for how many weeks are you going to add:(1 - 1 week, 2 - 2 weeks, 3 - weeks, 4 - one month");
scanf("%d",&x);
}while(i<0 || i>4);


for(i=0;i<x;i++) {
    struct Node* temp=(struct Node*)malloc(sizeof(struct Node));



    printf("Enter temperatures for week[%d]: ",k);

        for(z=0;z<7;z++) {
            fflush(stdin);
            printf("\nDay[%d]: ",z+1);
            scanf("%lf",&temperatures[z]);
        }



    if(head==NULL) {
        head = temp;
        current = head;
    }
    else
    {
        current->next = temp;
        current = temp;
    }
        strcpy(current->temperature,temperatures);


        for(f=0;f<7;f++){
            if(temperatures[f] < temperatures[f+1]) current->tempMin = temperatures[f];
            if(temperatures[f] > temperatures[f+1]) current->tempMax = temperatures[f];
            Sum = Sum + temperatures[f];
        }

        current->tempAvg = Sum/7;
        current->next = NULL;

k++;

    printf("Information added successfully.");

}

}

这是我的结构:

struct Node {
    float temperature[8];
    float tempMin;
    float tempMax;
    float tempAvg;
    struct Node* next;

};

我也尝试使用指针,它不起作用(增加时出错)。

1 个答案:

答案 0 :(得分:2)

使用memcpy代替,strcpy用于复制字符串arrys。