简体   繁体   English

我的代码给了我一个分段错误

[英]My code is giving me a segmentation fault

In my code I get a segmentation fault every time I try to run it.在我的代码中,每次尝试运行时都会出现分段错误。 I have tried to comment out the majority of the code and only leave the initialize function and function call in the code so I believe that the segmentation fault is due to that function.我试图注释掉大部分代码,只在代码中留下初始化函数和函数调用,所以我相信分段错误是由该函数引起的。 However I do not know how to fix the error after trying for a while.但是,我尝试了一段时间后不知道如何修复错误。 I am including all of the code in case there is something from another function that is causing the fault and I am incorrect.我包含了所有代码,以防其他函数中的某些内容导致错误并且我不正确。

#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++;
}

You misunderstand the way variables are passed by reference, look at this fixed version :你误解了变量通过引用传递的方式,看看这个固定版本:

#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++;
}

You never actually allocate memory for one and two so they have space to hold integer data, they're just null pointers.您实际上从未为onetwo分配内存,因此它们有空间来保存整数数据,它们只是空指针。 If your declare them as int so there's memory for the value (and use them accordingly), you should be halfway there.如果您将它们声明为int以便该值有内存(并相应地使用它们),那么您应该已经完成​​了一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM