首次执行后,地址/随机数的值会发生变化

时间:2013-04-21 07:10:40

标签: c memory-address

我是C的新手并且正在开发一个C程序,其中一部分需要我更改结构的值。我自己测试了它并且第一次工作。但是当我再次运行该函数时,它给出了数字似乎是它的地址。是否有解决方案或解决方法?我需要能够将值递增到10。

功能:

void bufferchange( csv_line line, Chart* record,Chart currecord )
{
    int numFlag = atoi(line.field[2]);

    printf("%i\n", record->buffer[numFlag-1].end );
    record->buffer[numFlag-1].end++;
    printf("%i\n", record->buffer[numFlag-1].end );
}

输出:

(input/function calling)
0
1
(second input/function calling)
1308655328
1308655329

根据要求:

#include <stdio.h>
#include <ctype.h>

#define MAXPATIENTS 5
#define MAXREADINGS 10
#define MAXTYPES 5
#define MAXTIME 8
#define MAX_FIELDS 5
#define MAX_CHARS 20

typedef enum { false, true } bool ;

typedef char f_string[MAX_CHARS+1];

typedef struct {
    int nfields ;                           /* 0 => end of file */
    f_string field[MAX_FIELDS] ;            /* array of strings for fields */
} csv_line ;

bool is_end_of_field(char ch) { //boolean
    return (ch == ',') || (ch == '\n') || (ch == EOF) ;
}

/* One health type reading: timestamp + actual value */
typedef struct{
    char timestamp[MAXTIME+1];
    int value;
}Element;

/* Circular buffer of health type readings */
typedef struct{
    int start;      /* index of oldest reading */
    int end;        /* index of most current reading */
    Element reading[MAXREADINGS];
}CircularBuffer;

/* Patient's health chart: ID + multiple health type readings */
typedef struct{
    int id;
    CircularBuffer buffer[MAXTYPES+1];
}Chart;

Chart record[MAXPATIENTS];

for( i=0; i < MAXPATIENTS; i++ ){ //initialization
record[i].id = i + 1;
for( j=0; j < MAXTYPES; j++ ){
    record[i].buffer[j].start = 0;
    record[i].buffer[j].end = 0;
    }
}

0 个答案:

没有答案