[英]My code is giving me a segmentation fault
在我的代码中,每次尝试运行时都会出现分段错误。 我试图注释掉大部分代码,只在代码中留下初始化函数和函数调用,所以我相信分段错误是由该函数引起的。 但是,我尝试了一段时间后不知道如何修复错误。 我包含了所有代码,以防其他函数中的某些内容导致错误并且我不正确。
#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++;
}
你误解了变量通过引用传递的方式,看看这个固定版本:
#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++;
}
您实际上从未为one
和two
分配内存,因此它们有空间来保存整数数据,它们只是空指针。 如果您将它们声明为int
以便该值有内存(并相应地使用它们),那么您应该已经完成了一半。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.