C编程中分解指针错误
Factoring Pointer Error in C programming
所以我在制作一个要求用户输入数字然后使用该数字的程序时遇到了麻烦,我必须增加指向 two_count 和 three_count 的指针的值。这些是输入数字中二和三的反因数。
比如用户输入2,那么程序应该吐出
"There have been 1 factor of 2 and 0 factors of 3"
然后用户可以输入0退出程序
我目前拥有的是
include <stdio.h>
void main()
{
int* two_count;
int* three_count;
int num;
while(two_count >= 0 || three_count >= 0)
{
printf("Enter a number: \n");
scanf("%d", &num);
if(num % 2)
{
two_count++;
}
else if(num % 3)
{
three_count++;
}
else if(num == 0)
{
printf("Thank you for playing, enjoy your day!\n");
break;
}
printf("So far, there have been %d factors of 2 and %d factors of 3\n", two_count, three_count);
}
}
谢谢!
如果你想使用指针,你可以这样做
int two_count = 0;
int* two_count_ptr = &two_count;
int three_count = 0;
int* three_count_ptr = &three_count;
然后,为了检索值和递增,您需要取消引用指针
while(*two_count_ptr >= 0 || *three_count_ptr >= 0)
(*two_count_ptr)++;
希望对您有所帮助。
所以我在制作一个要求用户输入数字然后使用该数字的程序时遇到了麻烦,我必须增加指向 two_count 和 three_count 的指针的值。这些是输入数字中二和三的反因数。
比如用户输入2,那么程序应该吐出 "There have been 1 factor of 2 and 0 factors of 3" 然后用户可以输入0退出程序
我目前拥有的是
include <stdio.h>
void main()
{
int* two_count;
int* three_count;
int num;
while(two_count >= 0 || three_count >= 0)
{
printf("Enter a number: \n");
scanf("%d", &num);
if(num % 2)
{
two_count++;
}
else if(num % 3)
{
three_count++;
}
else if(num == 0)
{
printf("Thank you for playing, enjoy your day!\n");
break;
}
printf("So far, there have been %d factors of 2 and %d factors of 3\n", two_count, three_count);
}
}
谢谢!
如果你想使用指针,你可以这样做
int two_count = 0;
int* two_count_ptr = &two_count;
int three_count = 0;
int* three_count_ptr = &three_count;
然后,为了检索值和递增,您需要取消引用指针
while(*two_count_ptr >= 0 || *three_count_ptr >= 0)
(*two_count_ptr)++;
希望对您有所帮助。