int main(void){
int range = 0, i = 0;
printf("Enter the number of digits of the number: ");
scanf("%d", &range);
int a[range];
int b[range];
printf("Enter the number: ");
for(i = 0; i < range; i++){
scanf("%1d", &a[i]);
}
replace(a, b, range);
swap(&b[0],&b[range]);
printf("Output: ");
for(i = range; i > 0; i--){
printf("%d", b[i]);
}
return 0;
}
void replace(int *a, int *b, int n){
int *p;
int temp;
for(p = a; p < a + n; p++){
temp = ((*p + 6) % 10) / 10;
p = b;
*p = temp;
}
}
void swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
我的问题是第一个 for 循环,该循环似乎一直在接收输入而没有结束。我尝试在 replace 方法之前放置一个 print 语句,但它没有打印出来,所以我知道问题出在 for 循环上。我该如何解决这个问题?
最佳答案
replace()
函数是一场灾难
您现在发布了 replace
函数,这是一场灾难。
void replace(int *a, int *b, int n)
{
int *p;
int temp;
for (p = a; p < a + n; p++)
{
temp = ((*p + 6) % 10) / 10;
p = b;
*p = temp;
}
}
您尝试通过使 p
依次指向每个元素来遍历数组 a
。但是您随后在循环体中将 b
分配给 p
,这将它放在数组 a
之外,这意味着所有的赌注都是离开。不清楚 b
是否小于 a + n
— 它只是没有定义。但是考虑到你得到一个无限循环,它可能是,所以你的代码一遍又一遍地读取和写入相同的几个位置(b[0]
,b[1]
) 和 p
永远不会接近 a + n
。
一个简单的修复使用索引:
void replace(int *a, int *b, int n)
{
for (int i = 0; i < n; i++)
b[i] = ((a[i] + 6) % 10) / 10;
}
如果你想使用指针,那么:
void replace(int *a, int *b, int n)
{
for (int *p = a; p < a + n; p++)
*b++ = ((*p + 6) % 10) / 10;
}
请注意,表达式的计算结果为零。模 10 运算产生 0..9(或 -9..+9)范围内的值,除以 10 始终为 0。您需要处理该表达式。
对 swap()
的调用中断了
你有:
swap(&b[0], &b[range]);
这肯定会访问 b
数组范围之外的数据。为了安全起见,您需要使用:
swap(&b[0], &b[range-1]);
你的输出循环坏了
你有:
printf("Output: ");
for(i = range; i > 0; i--){
printf("%d", b[i]);
}
需要避免再次访问b[range]
,最后需要输出一个换行符:
printf("Output: ");
for (i = range; i > 0; i--)
printf("%d", b[i-1]);
putchar('\n');
输入代码有效
输入代码有效,正如您所获得内容的这种最小改编所证明的那样:
#include <stdio.h>
int main(void)
{
int range = 0, i = 0;
printf("Enter the number of digits in the number: ");
if (scanf("%d", &range) != 1)
{
fprintf(stderr, "Oops 1\n");
return 1;
}
printf("Number of digits: %d\n", range);
int a[range];
printf("Enter the number: ");
for (i = 0; i < range; i++)
{
if (scanf("%1d", &a[i]) != 1)
{
fprintf(stderr, "Oops 2\n");
return 2;
}
printf("Digit %d: %d\n", i, a[i]);
}
printf("Reversed input: ");
for (i = range; i > 0; i--)
printf("%2d", a[i-1]);
putchar('\n');
return 0;
}
“反向输入”循环是对问题中“输出”循环的改编——修复了错误以避免越界访问数组,并使用 a
而不是 b
。错误消息的信息量非常小(不适合生产工作),但它们足以在您调试时确定是哪个语句导致了错误。
运行示例:
$ ./example-input
Enter the number of digits in the number: 12
Number of digits: 12
Enter the number: 1234 5678 9101112
Digit 0: 1
Digit 1: 2
Digit 2: 3
Digit 3: 4
Digit 4: 5
Digit 5: 6
Digit 6: 7
Digit 7: 8
Digit 8: 9
Digit 9: 1
Digit 10: 0
Digit 11: 1
Reversed input: 1 0 1 9 8 7 6 5 4 3 2 1
$
现在,将它应用到您的程序中,看看问题到底出在哪里。
工作代码
#include <stdio.h>
static void swap(int *a, int *b);
static void replace(int *a, int *b, int n);
int main(void)
{
int range = 0, i = 0;
printf("Enter the number of digits in the number: ");
if (scanf("%d", &range) != 1)
{
fprintf(stderr, "Oops!\n");
return 1;
}
printf("Number of digits: %d\n", range);
int a[range];
int b[range];
printf("Enter the number: ");
for (i = 0; i < range; i++)
{
if (scanf("%1d", &a[i]) != 1)
{
fprintf(stderr, "Oops 2\n");
return 2;
}
printf("Digit %d: %d\n", i, a[i]);
}
printf("Reversed input: ");
for (i = range; i > 0; i--)
printf("%2d", a[i-1]);
putchar('\n');
replace(a, b, range);
swap(&b[0], &b[range-1]);
printf("Output: ");
for (i = range; i > 0; i--)
printf("%2d", b[i-1]);
putchar('\n');
return 0;
}
static void swap(int *p, int *q)
{
int t = *p;
*p = *q;
*q = t;
}
static void replace(int *a, int *b, int n)
{
for (int *p = a; p < a + n; p++)
*b++ = ((*p + 6) % 10);
}
示例输出
$ ./example-input
Enter the number of digits in the number: 9
Number of digits: 9
Enter the number: 123 456 789
Digit 0: 1
Digit 1: 2
Digit 2: 3
Digit 3: 4
Digit 4: 5
Digit 5: 6
Digit 6: 7
Digit 7: 8
Digit 8: 9
Reversed input: 9 8 7 6 5 4 3 2 1
Output: 7 4 3 2 1 0 9 8 5
$
关于c - 循环没有结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542537/