【发布时间】:2016-11-12 03:26:37
【问题描述】:
int findMax(int*sums){
int t = 0;
int max = sums[0];
while (sums[t] != '\0'){
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max ){
max = sums[t];
}
t ++;
}
return max;
}
这个输出:
current max: 7 7
current max: 7 4
current max: 7 2
它忽略了列表的其余部分,sums。我认为这是因为sums 中的下一个元素是0。但我不明白为什么它会将0 视为'\0' (null)。
【问题讨论】:
-
'\0'是一个int,其值为0。没有类型差异。它们无法区分。 -
它自动强制 - 0 变为 '\0' (也变为 null,这是不同类型的不同值)
-
没有
null int,请看其他cmets。 -
如前所述,'\0' 实际上只是零。您将 sums[] 视为一个以 null 结尾的数组。 Int 数组在 C 中不是以空值结尾的(因为无法判断数组中的 0 是数据元素还是终止字符)。 Null 终止适用于 char 数组,因为 '\0' 不是有效的可打印字符,因此不存在冲突。
-
@KarenMcCulloch 你的数组元素都是正数吗?或者您输入数组的元素是否有任何范围...
标签: c arrays while-loop int max