变量重新分配-C

变量重新分配-C,c,variables,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 基本上,我遇到的问题是,对于每个元素结构,它打印相同的时间戳,即上次输入的时间戳 有人能帮忙吗?如果我说得太简短了,我很抱歉,我不想让这篇文章过多。如果你需要更多的澄清,请让我知道 typedef struct{ char *timestamp; int value;

输入:

5, 08:00:00, 2, 30
5, 08:00:10, 6, 0
程序应输出:

Heart Rate:
08:00:00: 30
但它却输出:

Heart Rate:
08:00:10: 30
基本上,我遇到的问题是,对于每个元素结构,它打印相同的时间戳,即上次输入的时间戳

有人能帮忙吗?如果我说得太简短了,我很抱歉,我不想让这篇文章过多。如果你需要更多的澄清,请让我知道

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");
}
typedef结构{
字符*时间戳;
int值;
}元素;
typedef char fu字符串[MAXCHARS+1];
类型定义结构{
内陆地区;
f_字符串字段[最大字段];
}csv_线;
void main(){
int i,j;
csv_线数据;
int run=1;
元件温度;
temperature.timestamp=“\0”;
温度值=-1;
元素心率;
心率时间戳=“\0”;
心率值=-1;
元素收缩压;
收缩压。时间戳=“\0”;
收缩压值=-1;
元素舒张压;
舒张压。时间戳=“\0”;
舒张压值=-1;
元素呼吸速率;
呼吸速率。时间戳=“\0”;
呼吸速率值=-1;
/*初始化每个患者的健康数据记录*/
对于(i=0;i
使用此输入:

5, 08:00:00, 2, 30
5, 08:00:10, 6, 0
和假设(大假设,因为未提供
get\u 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
然后
案例2:
会导致:

 heart_rate.timestamp = timestamp; = pointer to data.field[1]
 heart_rate.value = value;         = 30
然后将下一行读取到与第一行相同的内存中

然后。。。调用print_data():包含以下两行:

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

这就是为什么会出现意外的输出

要解决此问题,请执行以下操作:

char *timestamp = data.field[1];
需要更改为:

char *timestamp = strdup(data.field[1]);
当然,这会增加更多的代码要求:

1) check (!=NULL) the returned value from `strdup()` 
    to assure the operation was successful.
2) before exiting the program, need to pass each 'timestamp' to `free()`

free( heart_rate.timestamp );
通过此输入:

5, 08:00:00, 2, 30
5, 08:00:10, 6, 0
和假设(大假设,因为未提供
get\u 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
然后
案例2:
会导致:

 heart_rate.timestamp = timestamp; = pointer to data.field[1]
 heart_rate.value = value;         = 30
然后将下一行读取到与第一行相同的内存中

然后。。。调用print_data():包含以下两行:

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

这就是为什么会出现意外的输出

要解决此问题,请执行以下操作:

char *timestamp = data.field[1];
需要更改为:

char *timestamp = strdup(data.field[1]);
当然,这会增加更多的代码要求:

1) check (!=NULL) the returned value from `strdup()` 
    to assure the operation was successful.
2) before exiting the program, need to pass each 'timestamp' to `free()`

free( heart_rate.timestamp );

复制指针并不会复制指针指向的内存。那么我应该怎么做呢?如果不了解指针是如何定义数据结构的,很难给出建议。但这是一个更大的话题。在第一次“hello world”课程之后,您几乎需要立即了解指针和内存的基本知识。这个问题更多的是关于“我如何用C编程”而不是“我如何解决我的问题”;我仍在学习和试验它们。我已经添加了正在使用的结构。为了便于我们人类阅读和理解:1)一致缩进,在每个开始大括号“{”之后缩进。在每个结束大括号“}”之前取消缩进。切勿使用制表符缩进。建议每个缩进级别为4个空格,因为即使使用可变宽度字体也可以看到。2) 单独的代码块(用于、if、else、while、do…while、switch、,