[英]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.