什么导致这个 while 函数终止?

问题描述 投票:0回答:1

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

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

但是,当我运行该程序时,它会在我输入要访问的数组编号的位置终止。这是因为你不能用

scanf
函数改变iChoice的值吗?我必须承认,除了用户输入值外,
iChoice
根本没有被初始化。

在我重写我的程序之前,我尝试使用一个

while
循环来代替当前的
do while
和一个
if
语句,其中当前的
while
是。我认为它会正确地转移程序控制,但显然你需要
if
中的布尔值或其他东西。那没有用。

c while-loop do-while
1个回答
0
投票

许多问题(很难说OP在写它时有什么问题):

  1. *iArray = iNumber;
    您将未初始化整数转换为指针的值分配给指针。分配没有意义,还会调用未定义的行为。

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

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

  4. iChoice = NULL
    你将整数与指针进行比较。

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

    break 将在 scanf 之后终止此循环。

    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
© www.soinside.com 2019 - 2024. All rights reserved.