c - 如何在我的 C 代码中修复此段错误

标签 c

我为我的大学编程课编写了一个代码,它旨在使用名为 ds4rd.exe 的程序从 Dualshock 4 Controller 收集数据。但是,代码似乎可以编译,但每当我尝试运行它时,我都会收到段错误。我该如何解决这个问题?

这是我的代码

// 185 lab6.c
//
// This is the outline for your program
// Please implement the functions given by the prototypes below and
// complete the main function to make the program complete.
// You must implement the functions which are prototyped below exactly
//  as they are requested.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589

//NO GLOBAL VARIABLES ALLOWED


//PRE: Arguments must point to double variables or int variables as appropriate
//This function scans a line of DS4 data, and returns
//  True when the square button is pressed
//  False Otherwise
//This function is the ONLY place scanf is allowed to be used
//POST: it modifies its arguments to return values read from the input line.
int read_line(double* g_x, double* g_y, double* g_z, int* time, int* Button_T, int* Button_X, int* Button_S, int* Button_C);

// PRE: -1.0 <= x_mag <= 1.0
// This function computes the roll of the DS4 in radians
// if x_mag outside of -1 to 1, treat it as if it were -1 or 1
// POST: -PI/2 <= return value <= PI/2
double roll(double x_mag);

// PRE: -1.0 <= y_mag <= 1.0
// This function computes the pitch of the DS4 in radians
// if y_mag outside of -1 to 1, treat it as if it were -1 or 1
// POST: -PI/2 <= return value <= PI/2
double pitch(double y_mag);


// PRE: -PI/2 <= rad <= PI/2
// This function scales the roll value to fit on the screen
// POST: -39 <= return value <= 39
int scaleRadsForScreen(double rad);

// PRE: num >= 0
// This function prints the character use to the screen num times
// This function is the ONLY place printf is allowed to be used
// POST: nothing is returned, but use has been printed num times
void print_chars(int num, char use);

//PRE: -39 <= number <=39
// Uses print_chars to graph a number from -39 to 39 on the screen.
// You may assume that the screen is 80 characters wide.
void graph_line(int number);


int main() {
    double x, y, z;                             // magnitude values of x, y, and z
    int b_Triangle, b_X, b_Square, b_Circle;    // variables to hold the button statuses
    double roll_rad, pitch_rad;                 // value of the roll measured in radians
    int scaled_value;   // value of the roll adjusted to fit screen display

    int time = 0;
    int run_Roll, run_Pitch;

    //insert any beginning needed code here

    do
    {
        // Get line of input
        read_line(&x, &y, &z, &time, &b_Triangle, &b_X, &b_Square, &b_Circle);

        // calculate roll and pitch.  Use the buttons to set the condition for roll and pitch
        roll_rad = roll(x);
        pitch_rad = pitch(y);

        if (b_Triangle == 1) {
            run_Roll = 1;
            run_Pitch = 0;
        }
        if (b_X == 1) {
            run_Roll = 0;
            run_Pitch = 1;
        }

        // switch between roll and pitch(up vs. down button)
        if (b_Triangle == 1) {
            run_Roll = 1;
        }
        if (b_X == 1) {
            run_Pitch = 1;
        }

        // Scale your output value
        roll_rad = scaleRadsForScreen(roll_rad);
        pitch_rad = scaleRadsForScreen(pitch_rad);

        // Output your graph line
        if (run_Roll == 1) {
            graph_line(roll_rad);
        }
        if (run_Pitch == 1) {
            graph_line(pitch_rad);
        }

        fflush(stdout);
    } while (b_Square != 1); // Modify to stop when the square button is pressed
    return 0;
}

int read_line(double* g_x, double* g_y, double* g_z, int* time, int* Button_T, int* Button_X, int* Button_S, int* Button_C) {
    double x, y, z;
    int b_Triangle, b_X, b_Square, b_Circle;
    scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &g_x, &g_y, &g_z, &Button_T, &Button_X, &Button_S, &Button_C);
    if (*Button_S == 1) {
        return(1);
    }
    else {
        return(0);
    }
}

double roll(double x_mag) {
    if (x_mag <= -1.0) {
        x_mag = -1.0;
    }
    if (x_mag >= 1.0) {
        x_mag = 1.0;
    }
    return(asin(x_mag));
}

double pitch(double y_mag) {
    if (y_mag <= -1.0) {
        y_mag = -1.0;
    }
    if (y_mag >= 1.0) {
        y_mag = 1.0;
    }
    return(asin(y_mag));
}

int scaleRadsForScreen(double rad) {
    return (int)((78 * rad) / PI);
}

void print_chars(int num, char use) {
    int i = 0;

    for (i = 0; i < num; i++) {
        printf("%c", use);
    }
    return;
}

void graph_line(int number) {
    if(number < 0) {
        print_chars(number + 39, ' ');
        print_chars(abs(number), 'l');
    }
    else if(number > 0) {   
        print_chars(40, ' ');
        print_chars(number, 'r');
    }
    else if(number == 0) {
        print_chars(39, ' ');
        print_chars(1, '0');
    }
}

最佳答案

我怀疑问题发生在 read_line 中的 scanf 处。您将所有参数传递给带有地址 & 的函数,但是,这些变量已经是指针,您需要在传递它们时不带符号。您还应该检查 scanfread_line 的返回值。

关于c - 如何在我的 C 代码中修复此段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842546/

相关文章:

c - c中的字符串处理

c - 是否可以按位分配动态内存?

c - fprintf 语句在代码块中未正确执行

c - 评估字符数组中给出的数学表达式

objective-c - 这安全吗? (指向 Objective C 实例变量的指针)

c - 线程工作不正常

c - 以跨平台方式查找 c 二进制函数名称的最简单方法是什么?

c - ASCII/计数打印C程序?

c++ - Arduino Uno - serial.read 到位结构

调试中的 C 函数崩溃(未给出错误)