当数组值为零且不为空时,c循环停止
这将产生:当数组值为零且不为空时,c循环停止,c,arrays,while-loop,int,max,C,Arrays,While Loop,Int,Max,这将产生: 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; } 它忽略了列表的其余部分,sums。我认为这是因为
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;
}
它忽略了列表的其余部分,
sums
。我认为这是因为sums
中的下一个元素是0
。但我不明白为什么它会将0
视为'\0'
(null)。sums
是一个整数数组(技术上是指向整数的指针)<代码>“\0”(空字节)和0是相同的值,因此循环遇到0时将停止。就整数而言,不存在空值。术语“null”用于指值null
,它是一个指针,通常值为0(即,不指向任何内容的指针),也指null(0)字节,例如出现在以null结尾的字符串末尾的字节。'\0'表示不可打印的ASCII字符。具体来说,它是字符0(如中所示,是零位字符,而不是字符“0”,即48。请在ASCII表中查找)
“\0”与“0”相同,就像“A”等于65一样。就编译器而言没有区别。”\0'==0将始终计算为true
请注意,与所有其他数组不同,只有字符串以“\0”结尾。在C中,字符文本
'\0'
的值为(int)0
,这就是转义序列的转换结果
current max: 7 7
current max: 7 4
current max: 7 2
#包括
int main(){
int i=0;
字符c='\0';
printf(“%s\n”,(i==c)?“相同”:“不同”);
}
我认为你把指针检查
NULL
与值检查零混淆了
下面是两个稍有不同的函数变体来说明这一点:
#include <stdio.h>
int main() {
int i = 0;
char c = '\0';
printf("%s\n", (i == c) ? "same" : "different");
}
#包括
int
findPtr(整数**总和)
{
int t=0;
int max=*和[0];
int-val;
while(总和[t]!=NULL){
val=*和[t];
printf(“当前最大值:%d%d\n”,最大值,val);
如果(val>max){
max=val;
}
t++;
}
返回最大值;
}
int
findArr(整数*和,整数计数)
{
int t=0;
int max=总和[0];
while(t最大值){
max=总和[t];
}
t++;
}
返回最大值;
}
由于零(相当于零的
0
或\0
)是求和中的有效值,因此它不能像检查时那样用作数组末尾的哨兵。您需要像后一个示例中那样向下传递数组计数。在您的代码中,将指向整数数组的指针作为findMax函数的输入\“0”是一个字符。您正在将整数与字符进行比较,导致编译器强制转换字符“\0”并使用其等效整数NULL(或仅0)。因此,当数组中出现0时,程序停止。
您可能要尝试:
#include <stdio.h>
int
findPtr(int **sums)
{
int t = 0;
int max = *sums[0];
int val;
while (sums[t] != NULL) {
val = *sums[t];
printf("current max: %d %d\n", max, val);
if (val > max) {
max = val;
}
t++;
}
return max;
}
int
findArr(int *sums,int count)
{
int t = 0;
int max = sums[0];
while (t < count) {
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max) {
max = sums[t];
}
t++;
}
return max;
}
int findMax(int*sums,int arraysize)
{
int t=0;
int max=总和[t];
while(t最大值)
{max=sums[t];}
t++;
}
返回最大值;
}
- 我确实记得我第一次遇到同样的问题时(当时我正试图使用
数组构建一个数组),最终我发现与其他答案所说的几乎相同,从技术上讲int
和'\0'
具有相同的值。0
- 下面是我用来克服这个问题的
方法,这些方法只适用于某些情况2
- 案例1:
- 现在,由于所有输入元素都是正数,因此可以通过插入负数来标记数组的结尾
- 通常,我使用
,方法如下:-1
int findMax(int*sums,int arraysize) { int t=0; int max = sums[t]; while(t<arraysize) { printf("current max: %d %d\n", max, sums[t]); if (sums[t] > max ) {max = sums[t];} t++; } return max; }
for(int index = 0; a[index] >= 0; index++) { //use the array element a[index] for desired purpose! }
- 相反,您可以输入任何负数,并按此方式执行
int a[] = {1, 2, 3, 4, -1} for(int index = 0; a[index] != -1; index++) { //use the array element a[index] for desired purpose! }
- 案例2:
- 您现在可能已经有了这个想法:),假设您的所有元素都属于范围
[-100100]
- 您可以在范围边界上方或下方插入任何数字以标记结束。。。因此,在上述情况下,我可以通过输入数字
和<-100
来标记结束>100
- 您可以这样迭代循环:
int findMax(int*sums,int arraysize) { int t=0; int max = sums[t]; while(t<arraysize) { printf("current max: %d %d\n", max, sums[t]); if (sums[t] > max ) {max = sums[t];} t++; } return max; }
for(int index = 0; a[index] >= 0; index++) { //use the array element a[index] for desired purpose! }
因此,现在在案例1下,while循环条件可以是以下任一情况:
在案例2下:for(int index = 0; a[index] != value_not_in_array; index++) { //use the array element a[index] for desired purpose! }
while(sums[t] != -1) //typically ended with `-1` //(or) while (sums[t] >= 0) //ended with any negative number
这两个案例的基本事实是我发现了一个 终止字符的潜在替换。
字符。'\0'
希望这有帮助,快乐编码;)
是一个值为'\0'
的0
。没有类型差异。它们无法区分。它会自动强制-0变为“\0”(也会变为null,这是不同类型的不同值)。没有int
,请参阅其他注释。如前所述,“\0”实际上只是零。您将sums[]视为一个以null结尾的数组。Int数组在C中不是以null结尾的(因为无法判断数组中的0是数据元素还是终止字符)。空终止适用于字符数组,因为“\0”不是有效的可打印字符,所以不存在冲突。@KarenMcCulloch数组的所有元素都是正数吗?或者您输入到数组中的元素有任何范围……因此,如果您不知道列表有多长,并且列表中可能有零,那么您可以在C中迭代int列表吗?在C中,无法确定由指针表示的数组的长度。在函数中,必须将长度作为附加参数传递。@Annonymus请参见6.4.4.4/2:“整数字符常量是一个或多个多字节字符的序列null int
while( sums[t] != value_not_in_array )