-3 c while-loop do-while
我正在尝试编写将指针变量分配给数组索引的代码。这是代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该程序时,它会在我输入要访问的数组编号时终止。这是因为您无法使用函数更改 iChoice 的值吗scanf?我必须承认,iChoice除了用户输入值之外,根本没有初始化。
在重写程序之前,我尝试使用while循环代替当前值do while和if当前值所在的语句while。我认为它会正确地转移程序控制,但显然你需要布尔值或if. 那行不通。
很多问题(很难说OP写它的初衷是什么):
*iArray = iNumber;您将未初始化的整数转换为指针的值分配给指针。赋值没有意义,还会引发未定义的行为。
iArray[x] = '\0';其中x<0,4>。与上面相同+访问边界之外的数组。另一个UB
printf("\nArray element #5: %d", iArray[4]);您尝试将指针打印为整数(错误格式== UB所有printfs)+访问超出范围(UB)
iChoice = NULL你分配而不是比较。NULL 是一种特殊类型的指针。
do {
printf("\n\t\tEnter a number to fill in: ");
scanf("%d", &iNumber);
break;
} while (iChoice != NULL);
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)