我一直在试图运行我的代码,但总是与错误代码0000005计划与0000005
#include <stdio.h>
#include <math.h>
void calculate_resistance(char metal, int length, int diameter, float resistivity);
int main()
{
int length, diameter;
float resistivity;
char metal;
printf("Enter the name of the metal: ");
scanf("%s", &metal);
printf("Enter the resistivity: ");
scanf("%f", &resistivity);
printf("Enter the length: ");
scanf("%d", &length);
printf("Enter the diameter: ");
scanf("%d", &diameter);
calculate_resistance(metal, length, diameter, resistivity);
return 0;
}
void calculate_resistance(char metal, int length, int diameter, float resistivity)
{
float radius = diameter/2;
float area_of_wire = (M_PI) * pow(radius,2) * length;
float resistance = resistivity * length/area_of_wire;
printf("Resistivity of %s is %f", metal, resistance);
}
我发现,如果我注释掉的“printf(”%S的电阻率是%F结束”,金属崩溃, 抵抗性);”或在最后一次scanf后的任何printf它不会崩溃与错误代码0xC0000005
'char metal;' - >'char metal [96];'(适当大小),'void calculate_resistance(char metal [],' – BLUEPIXY