是什么原因导致这个while函数终止?[已结束]

What is causing this while function to terminate? [closed]

提问人:c_and_me1 提问时间:3/11/2023 最后编辑:c_and_me1 更新时间:3/11/2023 访问量:70

问:


这个问题是由一个错别字或一个无法再重现的问题引起的。虽然类似的问题可能在这里成为主题,但这个问题的解决方式不太可能帮助未来的读者。

9个月前关闭。

我正在尝试编写将指针变量分配给数组索引的代码。代码如下:

#include <stdio.h>

int main()
{
    int x;
    int iChoice;
    int *iArray[4];
    int iNumber;
    *iArray = iNumber;

    for (x = 0; x < 5; x++) {
        iArray[x] = '\0';
    }

    do {
        system("clear");
        printf("\n\t\tENTER NUMBERS INTO AN ARRAY\n");
        printf("\nArray element #1: %d", iArray[0]);
        printf("\nArray element #2: %d", iArray[1]);
        printf("\nArray element #3: %d", iArray[2]);
        printf("\nArray element #4: %d", iArray[3]);
        printf("\nArray element #5: %d", iArray[4]);
        printf("\n\nEnter an array number to access: ");
        scanf("%d", &iChoice);
    } while (iChoice = NULL);

    do {
        printf("\n\t\tEnter a number to fill in: ");
        scanf("%d", &iNumber);
        break;
    } while (iChoice != NULL);
    return 0;
}

但是,当我运行该程序时,它会在我输入要访问的数组编号处终止。这是因为你不能用函数来改变iChoice的值吗?我必须承认,除了用户输入值外,根本没有初始化。scanfiChoice

在我重写我的程序之前,我试图使用一个循环来代替电流和一个电流所在的语句。我以为它会正确地转移程序控制,但显然你需要布尔值或 .那没有用。whiledo whileifwhileif

c 循环 do-while

评论

8赞 qrsngky 3/11/2023
什么时候应该有?iChoice = NULLiChoice == NULL
0赞 c_and_me1 3/11/2023
实际上,在你告诉我之前,我尝试过,这使我能够更改数组元素的值,但是在完成之后,代码终止。
0赞 c_and_me1 3/11/2023
好的,会更新。
0赞 Steve Summit 3/11/2023
什么是 ?它指向哪里?设置 很奇怪,然后转身将 (和 , , ...) 设置为 。iArray*iArray = iNumber;iArray[0]iArray[1]iArray[2]'\0'
1赞 Andreas Wenzel 3/11/2023
该行将写入越界,调用未定义的行为。对于大小为 的数组,有效索引为 ,但您使用的是 。iArray[x] = '\0';iArray40304

答:

6赞 0___________ 3/11/2023 #1

很多问题(很难说OP写它的想法):

  1. *iArray = iNumber;为指针分配值为“未初始化整数”的指针,该值将转换为指针。赋值毫无意义,并且还会调用未定义的行为。

  2. iArray[x] = '\0';其中 <0,4>。同上 + 访问边界外的数组。另一个UBx

  3. printf("\nArray element #5: %d", iArray[4]);您尝试将指针打印为整数(格式错误 == UB 所有 printfs)+ 超出边界的访问 (UB)

  4. iChoice = NULL你分配而不是比较。NULL 是一种特殊类型的指针。

  5. do {
        printf("\n\t\tEnter a number to fill in: ");
        scanf("%d", &iNumber);
        break;
    } while (iChoice != NULL);
    

    Break 将在 SCREF 之后终止此循环。甚至没有达到条件。while

始终阅读警告!!!您的代码会生成以下一堆:

<source>:12:13: warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
   12 |     *iArray = iNumber;
      |             ^
<source>:21:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
   21 |         printf("\nArray element #1: %d", iArray[0]);
      |                                     ~^   ~~~~~~~~~
      |                                      |         |
      |                                      int       int *
      |                                     %ls
<source>:22:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
   22 |         printf("\nArray element #2: %d", iArray[1]);
      |                                     ~^   ~~~~~~~~~
      |                                      |         |
      |                                      int       int *
      |                                     %ls
<source>:23:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
   23 |         printf("\nArray element #3: %d", iArray[2]);
      |                                     ~^   ~~~~~~~~~
      |                                      |         |
      |                                      int       int *
      |                                     %ls
<source>:24:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
   24 |         printf("\nArray element #4: %d", iArray[3]);
      |                                     ~^   ~~~~~~~~~
      |                                      |         |
      |                                      int       int *
      |                                     %ls
<source>:25:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
   25 |         printf("\nArray element #5: %d", iArray[4]);
      |                                     ~^   ~~~~~~~~~
      |                                      |         |
      |                                      int       int *
      |                                     %ls
<source>:28:22: warning: assignment to 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]
   28 |     } while (iChoice = NULL);
      |                      ^
<source>:28:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   28 |     } while (iChoice = NULL);
      |              ^~~~~~~
<source>:34:22: warning: comparison between pointer and integer
   34 |     } while (iChoice != NULL);
      |                      ^~
<source>:12:13: warning: 'iNumber' is used uninitialized [-Wuninitialized]
   12 |     *iArray = iNumber;
      |     ~~~~~~~~^~~~~~~~~
<source>:11:9: note: 'iNumber' declared here
   11 |     int iNumber;
      |         ^~~~~~~
Compiler returned: 0

评论

2赞 pm100 3/11/2023
#4 - 你的意思是“你把 NULL 分配给 ichoice”
0赞 0___________ 3/11/2023
@pm100确实如此,但如果我们考虑到代码的质量 - 这重要吗?:)