【问题标题】:c loop stops when array value is zero not null当数组值为零而不是空时,c循环停止
【发布时间】: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


【解决方案1】:
  • 我确实记得我第一次遇到同样问题的时候(当我尝试使用 int 数组构建 big number library 时),最终我想通了与其他答案所说的相同 technically '\0' 0 具有相同的价值。

  • 现在这里是2 我用来解决这个问题的方法,这些方法只适用于特定条件


  • 案例 1:

条件:当你所有的输入元素都是

  • 现在由于所有输入元素都是正数,您可以通过插入一个负数数字

  • 来标记数组的结尾
  • 通常,我使用-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!
    }
    
  • 相反,您可以输入任何负数并以这种方式进行

    for(int index = 0; a[index] >= 0; index++) 
    {
        //use the array element a[index] for desired purpose!
    }
    

  • 案例 2:

条件:当你的所有元素都绑定在某个范围

  • 您现在可能已经明白了 :),假设您的所有元素都属于 范围 [-100,100]

  • 您可以在范围边界上方或下方插入任何数字来标记结束...所以在上述情况下,我可以通过输入数字 < -100>100 来标记结束。

  • 你可以这样迭代循环:

    for(int index = 0; (a[index] > -100) && (a[index] < 100); index++) 
    {
        //use the array element a[index] for desired purpose!
    }
    

概括这两种情况,只需在数组末尾放置一个您确定不等于数组元素的值

    for(int index = 0; a[index] != value_not_in_array; index++) 
    {
        //use the array element a[index] for desired purpose!
    }

所以,现在在 案例 1 下,您的 while 循环条件可以是以下任一条件:

while(sums[t] != -1) //typically ended with `-1`
//(or)
while (sums[t] >= 0) //ended with any negative number

案例2下:

while ((sums[t] >min_range) && (sums[t] < max_range)) // when elements are bound within a range

或更笼统地说:

while( sums[t] != value_not_in_array )

这两种情况的根本事实是我发现了一个 终止的潜在替代品 '\0' character.

希望这会有所帮助,祝你编码愉快;)

【讨论】:

    【解决方案2】:

    在您的代码中,您将一个指向整数数组的指针作为 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;
    }
    

    【讨论】:

      【解决方案3】:

      我认为您将 NULL 的指针检查与零的值检查混淆了。

      这里有两个稍微不同的函数变体来说明这一点:

      #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;
      }
      

      由于零(0\0--它们都是等价的)是总和中的 有效 值,它不能用作 哨兵​​m>在您的检查中结束数组。您需要像后一个示例一样传递数组计数。

      【讨论】:

        【解决方案4】:

        '\0' 表示不可打印的 ASCII 字符。具体来说,是字符 0(如第 0 个字符,而不是字符 '0',即 48。在 ASCII 表中查找)。

        '\0' 与 0 相同,就像 'A' == 65 一样。就编译器而言,没有区别。 '\0' == 0 将始终评估为真。

        请注意,与所有其他数组不同,只有字符串以“\0”结尾。

        【讨论】:

          【解决方案5】:

          在 C 中,字符文字 '\0' 的值是 (int)0,这就是转义序列所转换的内容。

          #include <stdio.h>
          int main() {
              int i = 0;
              char c = '\0';
          
              printf("%s\n", (i == c) ? "same" : "different");
          }
          

          http://ideone.com/sYRbYZ

          【讨论】:

          • @Annonymus 请参阅 6.4.4.4/2:“整数字符常量是包含在单引号中的一个或多个多字节字符的序列,如 'x'。”
          • 我明白了。看来我被误导了。感谢您解决这个问题。
          【解决方案6】:

          sums 是一个整数数组(技术上是指向整数的指针)。 '\0'(空字节)和 0 是相同的值,因此当遇到 0 时循环将停止。就整数而言,没有空值之类的东西。术语“null”用于指代值NULL,它通常是一个值为0的指针(即不指向任何东西的指针),也是空(0)字节,例如出现在以 null 结尾的字符串末尾的那个。

          【讨论】:

          • 如果您不知道列表有多长并且列表中可能有零,您能否遍历 C 中的 int 列表?
          • 在 C 中,没有办法确定指针表示的数组的长度。在您的函数中,您必须将长度作为附加参数传递。
          猜你喜欢
          • 1970-01-01
          • 2014-11-23
          • 2013-06-13
          • 2014-12-31
          • 1970-01-01
          • 1970-01-01
          • 2021-10-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多