项目帮助 - 无法通过函数

时间:2017-04-25 04:06:26

标签: c pointers

我正在为学校开展一个项目。我们必须将两个数字分开并显示答案以及其余部分。函数中的值是正确的,但是当我试图通过main传递它时,它给出了错误的数字。谢谢你的帮助

#include <stdio.h>
void division (int number, int divisor, int *quotient, int *remainder);
int main(void)
{
    int i, d, q, r;
    int *quotient = &q;
    int *remainder = &r;
    printf("Enter an integer and divisor, separated by comma: ");
    scanf("%d, %d", &i, &d);

    division(i, d, quotient, remainder);

    printf("%d divided by %d: quotient = %d, remainder = %d\n", i, d, 
        *quotient, *remainder);
    return 0;
}

void division(int number, int divisor, int *quotient, int *remainder)
{
    int a = number / divisor;
    printf(" a = %d\n", a);
    int b = number % divisor;
    printf(" b = %d\n", b);
    quotient = *a;
    remainder = *b;
}

1 个答案:

答案 0 :(得分:0)

您正在分割功能中正确分配值。 尝试 * quotient = a; * remainder = b; 您正在通过引用进行传递,因此您需要将值分配给商和余数。