C 我的代码给了我一个分段错误
在我的代码中,我每次尝试运行它时都会遇到一个分段错误。我试着注释掉大部分代码,只在代码中留下初始化函数和函数调用,所以我相信分段错误是由于该函数造成的。然而,我不知道如何修复错误后,尝试了一段时间。我包括了所有的代码,以防有来自另一个函数的东西导致故障,而我是不正确的C 我的代码给了我一个分段错误,c,C,在我的代码中,我每次尝试运行它时都会遇到一个分段错误。我试着注释掉大部分代码,只在代码中留下初始化函数和函数调用,所以我相信分段错误是由于该函数造成的。然而,我不知道如何修复错误后,尝试了一段时间。我包括了所有的代码,以防有来自另一个函数的东西导致故障,而我是不正确的 #include <stdio.h> #include <string.h> // function prototypes void initialize(int* one, int* two, char*
#include <stdio.h>
#include <string.h>
// function prototypes
void initialize(int* one, int* two, char* ch);
void getHoursRate(double* pRate, double* hrs);
double payCheck(double rate, double hours);
void printCheck(double rate, double hours, double amount);
void funcOne(int* pOne, int pTwo);
void nextChar(char* ch);
int main()
{
int x, y;
char z;
double rate, hours;
double amount;
int* one = 0;
int* two = 0;
char* ch = NULL;
double* pRate = 0;
double* hrs = 0;
// call initialize here...
initialize(one, two, ch);
printf("After initialization: x = %d, y = %d, z = %c\n", *one, *two, *ch);
//call getHoursRate here...
getHoursRate(pRate, hrs);
rate = *pRate;
hours = *hrs;
//call payCheck here...
amount = payCheck(rate, hours);
//call printCheck here...
printCheck(rate, hours, amount);
x = 35;
y = 20;
printf("Before calling funcOne: x = %d, y = %d\n", x, y);
//call funcOne with x & y here...
funcOne(&x, y);
printf("After funcOne: x = %d\n", *one);
z = 'B';
printf("Before nextChar z = %c\n", z);
//nextChar with z here...
nextChar(ch);
printf("After nextChar: z = %c\n", *ch);
return 0;
}
void initialize(int* one, int* two, char* ch){
one = 0;
two = 0;
strcpy(ch, "~");
}
void getHoursRate(double* rate, double* hours){
printf("Enter hours worked: ");
scanf("%lf", hours);
printf("Enter pay rate: ");
scanf("%lf", rate);
}
double payCheck(double rate, double hours){
double amount = 0;
if (hours > 40){
amount += 40 * rate;
amount += (hours - 40) * rate * 1.5;
return amount;
}
else{
amount += 40 * rate;
return amount;
}
}
void printCheck(double rate, double hours, double amount){
printf("Hours worked: %lf\nPay Rate: %lf\nThis week's salary: %lf\n", hours, rate, amount);
}
void funcOne(int* pOne, int pTwo){
int temp;
printf("Enter an integer: ");
scanf("%d", &temp);
*pOne = (*pOne * 2) + pTwo - temp;
}
void nextChar(char* ch){
ch++;
}
#包括
#包括
//功能原型
无效初始化(int*1,int*2,char*ch);
无效小时率(双倍*pRate,双倍*hrs);
双倍工资(双倍费率,双倍工时);
作废打印支票(双倍费率、双倍小时、双倍金额);
无效函数(int*pOne,int pTwo);
void nextChar(char*ch);
int main()
{
int x,y;
charz;
双倍费率,小时;
双倍金额;
int*1=0;
int*two=0;
char*ch=NULL;
double*pRate=0;
双倍*小时=0;
//在这里调用初始化。。。
初始化(一,二,ch);
printf(“初始化后:x=%d,y=%d,z=%c\n”,*1,*2,*ch);
//在这里调用getHoursRate。。。
获取小时率(普拉特,小时);
比率=*普拉特;
小时=*小时;
//在这里打电话给payCheck。。。
金额=工资支票(费率,小时);
//在这里调用printCheck。。。
打印支票(费率、小时数、金额);
x=35;
y=20;
printf(“调用funcOne之前:x=%d,y=%d\n”,x,y);
//在这里用x&y调用funcOne。。。
funcOne&x,y;
printf(“在funcOne之后:x=%d\n”,*one);
z=‘B’;
printf(“在nextChar z=%c\n”,z之前);
//下一个是z。。。
nextChar(ch);
printf(“在nextChar:z=%c\n”之后,*ch);
返回0;
}
无效初始化(int*1,int*2,char*ch){
1=0;
二=0;
strcpy(ch,“~”);
}
void getHoursRate(双倍*费率,双倍*小时){
printf(“输入工作时间:”);
扫描频率(“%lf”,小时);
printf(“输入工资率:”);
扫描频率(“%lf”,速率);
}
双倍工资(双倍费率,双倍工时){
双倍金额=0;
如果(小时数>40){
金额+=40*费率;
金额+=(小时-40)*费率*1.5;
退货金额;
}
否则{
金额+=40*费率;
退货金额;
}
}
作废打印支票(双倍费率、双倍小时、双倍金额){
printf(“工作时间:%lf\n工资率:%lf\n本周工资:%lf\n”,小时、工资率、金额);
}
void funcOne(int*pOne,int pTwo){
内部温度;
printf(“输入一个整数:”);
scanf(“%d”、&temp);
*pOne=(*pOne*2)+pTwo-温度;
}
void nextChar(char*ch){
ch++;
}
实际上,您从未为一个和两个分配内存,因此它们有空间保存整数数据,它们只是空指针。如果您将它们声明为int
,以便有内存存储该值(并相应地使用它们),您应该已经完成了一半。您误解了通过引用传递变量的方式,请查看此固定版本:
#include <stdio.h>
#include <string.h>
// function prototypes
void initialize(int* one, int* two, char* ch);
void getHoursRate(double* pRate, double* hrs);
double payCheck(double rate, double hours);
void printCheck(double rate, double hours, double amount);
void funcOne(int* pOne, int pTwo);
void nextChar(char* ch);
int main()
{
int x, y;
char z;
double rate, hours;
double amount;
int one = 0; // ==== Variable must not be declared as pointers
int two = 0; // ==== They becomes pointers when you pass them
char ch = 0; // ==== to the function,
double pRate = 0;
double hrs = 0;
// call initialize here...
initialize(&one, &two, &ch); //<======= FIX HERE
printf("After initialization: x = %d, y = %d, z = %c\n", one, two, ch);
//call getHoursRate here...
getHoursRate(&pRate, &hrs); //<======= FIX HERE
rate = pRate;
hours = hrs;
//call payCheck here...
amount = payCheck(rate, hours);
//call printCheck here...
printCheck(rate, hours, amount);
x = 35;
y = 20;
printf("Before calling funcOne: x = %d, y = %d\n", x, y);
//call funcOne with x & y here...
funcOne(&x, y);
printf("After funcOne: x = %d\n", one);
z = 'B';
printf("Before nextChar z = %c\n", z);
//nextChar with z here...
nextChar(&ch);
printf("After nextChar: z = %c\n", ch);
return 0;
}
void initialize(int* one, int* two, char* ch){
one = 0;
two = 0;
if (ch) *ch = '~'; //<======= FIX HERE
}
void getHoursRate(double* rate, double* hours){
printf("Enter hours worked: ");
scanf("%lf", hours);
printf("Enter pay rate: ");
scanf("%lf", rate);
}
double payCheck(double rate, double hours){
double amount = 0;
if (hours > 40){
amount += 40 * rate;
amount += (hours - 40) * rate * 1.5;
return amount;
}
else{
amount += 40 * rate;
return amount;
}
}
void printCheck(double rate, double hours, double amount){
printf("Hours worked: %lf\nPay Rate: %lf\nThis week's salary: %lf\n", hours, rate, amount);
}
void funcOne(int* pOne, int pTwo){
int temp;
printf("Enter an integer: ");
scanf("%d", &temp);
if (pOne) *pOne = (*pOne * 2) + pTwo - temp;
}
void nextChar(char* ch){
ch++;
}
#包括
#包括
//功能原型
无效初始化(int*1,int*2,char*ch);
无效小时率(双倍*pRate,双倍*hrs);
双倍工资(双倍费率,双倍工时);
作废打印支票(双倍费率、双倍小时、双倍金额);
无效函数(int*pOne,int pTwo);
void nextChar(char*ch);
int main()
{
int x,y;
charz;
双倍费率,小时;
双倍金额;
int one=0;//==变量不能声明为指针
int two=0;//==当您传递它们时,它们将成为指针
char ch=0;//==到函数,
双pRate=0;
双小时=0;
//在这里调用初始化。。。
初始化(&one,&two,&ch);//代码中有大量空指针访问。