int findMax(int*sums){
int t = 0;
int max = sums[0];
while (sums[t] != '
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;
}
This outputs:
current max: 7 7
current max: 7 4
current max: 7 2
And its ignoring the rest of the list, sums. I think this is because the next element in sums is 0. But I can't see why it would treat 0 as '\0' (null).
它忽略了列表的其余部分,总结。我认为这是因为sums中的下一个元素是0.但我不明白为什么它会将0视为'\ 0'(null)。
6 个解决方案
#1
1
I do remember the time when I first encountered the same problem ( while I was trying to build a big number library using int arrays ), and eventually I figured out pretty much the same as what other answers say that technically '\0' and 0 have the same value.
我记得我第一次遇到同样问题的时候(当我试图使用int数组构建一个大数字库时),最终我发现了与其他答案说技术上'\ 0'和0的几乎相同具有相同的价值。
Now here are 2 ways that I used to overcome this problem and these are only applicable under certain conditions
现在有两种方法可以解决这个问题,这些方法仅适用于某些条件
- Case 1 :
情况1 :
Condition : When all your input elements are positive
条件:当所有输入元素都是正数时
Now since all your input elements are positive, you can mark the end of the array by inserting a negative number
现在,由于所有输入元素都是正数,因此可以通过插入负数来标记数组的结尾
Typically, I use -1, this way :
通常,我使用-1,这样:
int a[] = {1, 2, 3, 4, -1}
for(int index = 0; a[index] != -1; index++)
{
//use the array element a[index] for desired purpose!
}
Instead you can enter any negative number and do it this way
相反,您可以输入任何负数,并以这种方式执行
for(int index = 0; a[index] >= 0; index++)
{
//use the array element a[index] for desired purpose!
}
- Case 2 :
案例2:
Condition : When all your elements are bound within a certain range
条件:当所有元素都绑定在一定范围内时
You might have got the idea by now :), lets say that all your elements belong to the range [-100,100]
你可能现在已经有了这个想法:),让我们说你的所有元素都属于范围[-100,100]
you can insert any number above or below the bounds of the range to mark the end... so in the above case I can mark the end by entering a number < -100 and >100.
你可以在范围的边界之上或之下插入任何数字来标记结束...所以在上面的例子中我可以通过输入一个<-100和> 100的数字来标记结尾。
And you can iterate the loop this way :
你可以这样迭代循环:
for(int index = 0; (a[index] > -100) && (a[index] < 100); index++)
{
//use the array element a[index] for desired purpose!
}
Generalizing both the cases, just place a value at the end of array which you know for sure is not equal to an array element
对两种情况进行推广,只需在数组末尾放置一个值,您确定该值不等于数组元素
for(int index = 0; a[index] != value_not_in_array; index++)
{
//use the array element a[index] for desired purpose!
}
So, now under Case 1, your while loop condition can be either of the following :
所以,现在在案例1下,你的while循环条件可以是以下任一种:
while(sums[t] != -1) //typically ended with `-1`
//(or)
while (sums[t] >= 0) //ended with any negative number
And under Case 2 :
在案例2下:
while ((sums[t] >min_range) && (sums[t] < max_range)) // when elements are bound within a range
Or more generally :
或者更一般地说:
while( sums[t] != value_not_in_array )
The underlying fact of both the cases is that I'm finding out a potential replacement for terminating '\0' character.
两种情况的根本事实是,我发现了终止'\ 0'字符的潜在替代品。
Hope this helps, happy coding ;)
希望这有帮助,快乐编码;)
#2
3
sums is an array of integers (technically a pointer to integer). '\0' (the null byte) and 0 are the same value, so your loop will stop when it encounters a 0. There is no such thing as a null value as far as integers are concerned. The term "null" is used to refer to the value NULL, which is a pointer usually with the value 0 (i.e., a pointer that doesn't point to anything), and also the null (0) byte, such as the one that occurs at the end of a null-terminated string.
sums是一个整数数组(技术上是一个指向整数的指针)。 '\ 0'(空字节)和0是相同的值,因此当遇到0时,你的循环将停止。就整数而言,没有空值这样的东西。术语“null”用于表示NULL值,它通常是值为0的指针(即,指向不指向任何内容的指针),以及null(0)字节,例如发生在以null结尾的字符串的末尾。
#3
1
'\0' is a representation of a non-printable ASCII character. Specifically, it is the character 0 (as in, the zeroeth character, not the character '0', whichis 48. Look it up on an ASCII table).
'\ 0'表示不可打印的ASCII字符。具体来说,它是字符0(如,零字符,而不是字符'0',这是48。在ASCII表上查找)。
'\0' is the same as 0 the same way 'A' is == 65. There is no difference as far as the compiler is concerned. '\0' == 0 will always evaluate as true.
'\ 0'与0相同,'A'= = 65.就编译器而言,没有区别。 '\ 0'== 0将始终评估为真。
Note that only strings are terminated with a '\0', unlike all other arrays.
请注意,与所有其他数组不同,只有字符串以'\ 0'结尾。
#4
0
In C, the character literal '\0' has the value (int)0, that's what the escape sequence translates to.
在C中,字符文字'\ 0'的值(int)为0,这就是转义序列转换为的内容。
#include <stdio.h>
int main() {
int i = 0;
char c = '\0';
printf("%s\n", (i == c) ? "same" : "different");
}
#5
0
I think you're confusing a pointer check for NULL vs a value check for zero.
我认为你对NULL的指针检查和零检查的值进行了混淆。
Here are two slightly different variants of your function to illustrate the point:
以下是函数的两个略有不同的变体来说明这一点:
#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;
}
Since zero (either 0 or \0--which are equivalent) is a valid value in sums, it can't be used as a sentinel for end of array as your check was doing. You'll need to pass down the array count as in the latter example.
由于零(0或\ 0 - 相等)是总和中的有效值,因此它不能用作检查所做的数组末尾的标记。您需要传递数组计数,如后一示例所示。
#6
-1
In your code, you are taking a pointer to an array of integers as input in the findMax function. '\0' is a character. You are comparing integers to a character, causing the compiler to cast the character '\0' and use its integer equivalent NULL (or simply 0). Therefore the program stops when it comes to a 0 in the array. You might want to try :
在您的代码中,您将在findMax函数中将指向整数数组的指针作为输入。 '\ 0'是一个角色。您正在将整数与字符进行比较,从而导致编译器转换字符'\ 0'并使用其整数等效的NULL(或简单地为0)。因此,程序在数组中的0时停止。您可能想尝试:
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;
}
'){
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max ){
max = sums[t];
}
t ++;
}
return max;
}
int findMax(int*sums){
int t = 0;
int max =