好吧,所以我必须编写一个函数,使用指针将天转换为年、周和天。这是函数。
int convertTime(int days, int *y, int *w, int *d){
if (days < 0 || y == NULL || w == NULL || d == NULL){
printf("An error has occured\n");
return 1;
}else{
*y = days / 365;
*w = (days % 365) / 7;
*d = ((days % 365) / 7) % 7)
return 0;
}
}
这是主函数中我调用它的部分。
// Tests convertTime
int days = 1000;
int y2 = 0, w2 = 0, d2 = 0;
int *y = NULL, *w = NULL, *d = NULL;
y = &y2, w = &w2, d = &d2;
convertTime(days, y, w, d);
printf("Expected output: 2 years, 38 weeks, 4 days\n");
printf("Actual output: %d years, %d, weeks, %d days\n");
然后打印出来
Expected output: 2 years, 38 weeks, 4 days
Actual output: -127184896 years, -132560896, weeks, -135499072 days
最佳答案
您忘记将变量传递给 printf。像这样修复最后一行:
printf("Actual output: %d years, %d, weeks, %d days\n", y2, w2, d2);
关于c - 为什么我会得到这些随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351337/