为什么 main 中的调试打印语句与每个子函数中的调试打印语句产生的结果不同?调试语句不缩进。谢谢!
代码:
//function declarations
void getData (char vehicleType);
void getVType (char* vehicleType);
int main()
{
//local declarations
char vehicleType; //type of vehicle - user input
//statements
getData (vehicleType);
printf("\nin main vehicleType: %c\n", vehicleType);
return 0;
}
void getData (char vehicleType)
{
getVType(&vehicleType);
printf("\nin getData vehicleType: %c\n", vehicleType);
}
void getVType(char* vehicleType)
{
printf("vehicle type: ");
scanf("%c", vehicleType);
printf("\nin getVType, you entered: %c\n", *vehicleType);
}
输出:
vehicle type: c
in getVType, you entered: c
in getData vehicleType: c
in main vehicleType:
Process returned 0 (0x0) execution time : 1.949 s
Press any key to continue.
最佳答案
在 main
中,vehicleType
从未设置过,因此它的值未定义。其他方法之所以有效,是因为您是通过引用(指针)而不是值传递的。
要将值返回到 main,您需要返回它
c = getData();
char getData ()
{
char vehicleType
getVType(&vehicleType);
printf("\nin getData vehicleType: %c\n", *vehicleType);
return vehicleType;
}
或者通过引用传递
getData(&c);
void getData (char *vehicleType)
{
getVType(vehicleType);
printf("\nin getData vehicleType: %c\n", *vehicleType);
}
关于c - 为什么 main 中的调试语句与每个子函数中的调试语句产生的结果不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751968/