c - 项目帮助-无法通过函数传递值

标签 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;
}

最佳答案

您在除法函数中正确分配了值。 尝试 *商=一个; *余数= b; 您正在进行按引用传递,因此您需要将值分配给商和余数。

关于c - 项目帮助-无法通过函数传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601394/

相关文章:

损坏的 ints 和 sscanf——以及其他与 C 相关的内存问题

c - C 中的 Telnet 服务器和客户端代码

c - strtok 未处理的异常;写入位置访问冲突

C:char指针和数组的区别

c++ - 在特定位置用元素填充数组

c - 在 C 中的二维字符数组中存储和返回本地指针

c - 10x10 数组上的随机游走

当作为另一个数组的索引传递时更改数组元素

ios - Swift 5 的 UnsafeMutablePointer 警告

c++ - 带有类指针的 if 语句导致段错误