可变重新分配 - C.

时间:2016-03-01 03:32:18

标签: c variables

输入:

5, 08:00:00, 2, 30
5, 08:00:10, 6, 0

程序应输出:

Heart Rate:
08:00:00: 30

但它输出:

Heart Rate:
08:00:10: 30

基本上,我遇到的问题是,对于每个Element结构,它会打印相同的时间戳,即上次输入的时间戳。

有人可以帮忙吗?对不起,如果我太简短了,我不想超载这篇文章。如果您需要更多说明,请告诉我。

typedef struct{
    char *timestamp;
    int value;
}Element;

typedef char f_string[MAXCHARS + 1];

typedef struct {
    int nfields;
    f_string field[MAXFIELDS];
} csv_line;

void main(){
    int i, j;
    csv_line data;
    int run = 1;
    Element temperature;
    temperature.timestamp = "\0";
    temperature.value = -1;
    Element heart_rate;
    heart_rate.timestamp = "\0";
    heart_rate.value = -1;
    Element systolic_pressure;
    systolic_pressure.timestamp = "\0";
    systolic_pressure.value = -1;
    Element diastolic_pressure;
    diastolic_pressure.timestamp = "\0";
    diastolic_pressure.value = -1;
    Element respiration_rate;
    respiration_rate.timestamp = "\0";
    respiration_rate.value = -1;

    /* initialize health data records for each patient */

    for( i=0; i < MAXPATIENTS; i++ ){
            record[i].id = i + 1;
            for( j=0; j < MAXTYPES; j++ ){
                record[i].buffer[j].start = 0;
            record[i].buffer[j].end = 0;
            }
        }   
    printf("Welcome to the Health Monitoring System\n\n");

    while (run == 1) {  

        data = get_line();

        char *timestamp = data.field[1];
        int value = atoi(data.field[3]);
        int type = atoi(data.field[2]);

        switch(type) {
            case 1:
                temperature.timestamp = timestamp;
                temperature.value = value;
                break;
            case 2:
                heart_rate.timestamp = timestamp;
                heart_rate.value = value;
                break;
            case 3:
                systolic_pressure.timestamp = timestamp;
                systolic_pressure.value = value;
                break;
            case 4:
                diastolic_pressure.timestamp = timestamp;
                diastolic_pressure.value = value;
                break;
            case 5:
                respiration_rate.timestamp = timestamp;
                respiration_rate.value = value;
                break;
            case 6:
                run = 0;
                break;
        }   
    }
    print_data(atoi(data.field[0]), temperature, heart_rate, systolic_pressure, diastolic_pressure,
        respiration_rate);

    printf("\nEnd of input\n");
}
void print_data(int id, Element temperature, Element heart_rate, Element systolic_pressure,
            Element diastolic_pressure, Element respiration_rate) {
    printf("-----------------------------------------------------------------------\n");
    printf("Readings for Patient ID = %d are:\n", id);
    printf("Temperature:\n");
    printf("%s: %.1f\n", temperature.timestamp, (float)temperature.value / 10);
    printf("Heart Rate:\n");
    printf("%s: %d\n", heart_rate.timestamp, heart_rate.value);
    printf("Systolic Pressure\n");
    printf("%s: %d\n", systolic_pressure.timestamp, systolic_pressure.value);
    printf("Diastolic Pressure\n");
    printf("%s: %d\n", diastolic_pressure.timestamp, diastolic_pressure.value);
    printf("Respiration Rate\n");
    printf("%s: %d\n", respiration_rate.timestamp, respiration_rate.value);
    printf("-----------------------------------------------------------------------\n");
}

1 个答案:

答案 0 :(得分:0)

使用此输入:

int

并假设(由于未提供5, 08:00:00, 2, 30 5, 08:00:10, 6, 0 的代码而给出了很大的假设)

第一次调用get_line()会产生包含以下内容的数据:

get_line()

然后这三行:

4, { "5", "08:00:00", "2", "30" }


data.field[0][] = "5"
data.field[1][] = "08:00:00" 
data.field[2][] = "2"
data.field[3][] = "30"

结果:

char *timestamp = data.field[1];
int value = atoi(data.field[3]);
int type = atoi(data.field[2]);

然后timestamp contains a pointer to data.field[1] value contains: 30 type contains: 2 会导致:

case 2:

然后读取下一行,与第一行相同的存储器

然后......对print_data()的调用:包含这两行:

 heart_rate.timestamp = timestamp; = pointer to data.field[1]
 heart_rate.value = value;         = 30

其中'heart_rate.timestamp'仍然指向printf("Heart Rate:\n"); printf("%s: %d\n", heart_rate.timestamp, heart_rate.value); 中的内存位置,该位置现在包含第二行的信息。

这就是意外输出的原因。

解决此问题:此行:

data

需要更改为:

char *timestamp = data.field[1];

当然,这会增加一些代码要求:

char *timestamp = strdup(data.field[1]);