提问人:c_and_me1 提问时间:3/11/2023 最后编辑:c_and_me1 更新时间:3/11/2023 访问量:70
是什么原因导致这个while函数终止?[已结束]
What is causing this while function to terminate? [closed]
问:
我正在尝试编写将指针变量分配给数组索引的代码。代码如下:
#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的值吗?我必须承认,除了用户输入值外,根本没有初始化。scanf
iChoice
在我重写我的程序之前,我试图使用一个循环来代替电流和一个电流所在的语句。我以为它会正确地转移程序控制,但显然你需要布尔值或 .那没有用。while
do while
if
while
if
答:
6赞
0___________
3/11/2023
#1
很多问题(很难说OP写它的想法):
*iArray = iNumber;
为指针分配值为“未初始化整数”的指针,该值将转换为指针。赋值毫无意义,并且还会调用未定义的行为。iArray[x] = '\0';
其中 <0,4>。同上 + 访问边界外的数组。另一个UBx
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);
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确实如此,但如果我们考虑到代码的质量 - 这重要吗?:)
评论
iChoice = NULL
iChoice == NULL
iArray
*iArray = iNumber;
iArray[0]
iArray[1]
iArray[2]
'\0'
iArray[x] = '\0';
iArray
4
0
3
0
4