【发布时间】:2016-02-09 11:30:00
【问题描述】:
我有一个包含 15 个元素的数组,并且在每个 while 循环通过时,最后一个值都会更改,或者至少它显然是错误的。有时它会增加,有时它只是跳转到另一个可能在程序控制下的内存地址值。首先,我将所有值归零。代码:
int w16 = 0;
int len[15];
for (i = 0; i < 16; ++i){ //zero the array
len[i] = 0;
}
while ((c = fgetc(file)) != EOF) {
printf("while: %d \n", len[15]);
if(isspace(c)){
word = 0;
if (0 < letters && letters < 16){
len[letters] = len[letters] + 1;
letters = 0;
} else if(letters > 15){
++w16;
letters = 0;
}
}else if (word == 0) {
word = 1;
++letters;
++nw;
} else if (word == 1) {
++letters;
}
}
printf 命令的一些输出。我看不出在上面的代码中它在哪里被改变了,为什么只有最后一个,其他的都很好:
while: 111
while: 110
while: 101
while: 32
while: 49
while: 50
while: 51
while: 52
while: 53
while: 54
while: 55
while: 56
while: 57
while: 115
Before fclose: -1
关于这里可能有什么问题的任何想法?除了这个讨厌的错误之外,其他一切都很好。我最近从 Java 迁移到 C,我也可能遗漏了一些东西。
【问题讨论】:
标签: c arrays file while-loop