如何将结构作为参数传递给C中的错误检查函数?

时间:2018-04-01 12:17:44

标签: c function structure

代码的一般说明: -

     It is a simple program in C using structures to get the input from    
     the user and print the output.Both input and output are defined as    
     separate functions in separate files. Additional user defined error 
     header file is created for error handling which checks error of C 
     function calls, 'malloc' in this case.

环境:Linux,操作系统:fedora

错误


    input.c: In function ‘input’:
    input.c:17:8: error: incompatible type for argument 1 of ‘error’
      error(*val, -1, "malloc");    //error because of this line //usage error(int return_variable, value, "func_name")
            ^
    In file included from input.c:3:0:
    error.h:7:6: note: expected ‘int’ but argument is of type ‘struct Eq’
     void error(int val, int ret, char* func_name)
          ^~~~~
    make: *** [Makefile:10: input.o] Error 1

的main.c

//********* headers **********//
#include<stdio.h>
#include<stdlib.h>

//********** structure *********//
struct Eq
{
    int *x;
    int *y;
};

//********** Function prototypes ***********//

struct Eq* input();             
int output(struct Eq*);


//*********** Main function *************//
int main()
{
    struct Eq* num;

    num = input();          //takes user input, returns to num

    output(num);            //prints the output, num passed as arg.

    return 0;
}

input.c 输入功能

#include<stdio.h>
#include<stdlib.h>
#include"error.h"                   //user defined header file

//********** structure *********//
struct Eq
{
    int *x;
    int *y;
};

struct Eq* input()
{
    struct Eq *val;

    val = (struct Eq*)malloc(sizeof(struct Eq));
    error(*val, -1, "malloc");          //error because of this line    //usage error(int return_variable, value, "func_name")

    val->x = (int*)malloc(sizeof(int));
// *val->x = -1;                    //!! try changing this and func below to invoke error function !!
    error(*val->x, -1, "malloc");           //no errors because of this

    val->y = (int*)malloc(sizeof(int));
    error(*val->y, -1, "malloc");           //no errors because of this

    printf("Enter the value of x:");
    scanf("%d", val->x);

    printf("Enter the value of y:");
    scanf("%d", val->y);

    return val;
}

output.c

#include<stdio.h>
#include<stdlib.h>

//********** structure *********//
struct Eq
{
        int *x;
        int *y;
};

int output(struct Eq *num)
{
        printf("value of x=%d\n",*num->x);
        printf("value of y=%d\n",*num->y);

        return 0;
}

error.h 错误处理程序头文件

#include<stdio.h>
#include<stdlib.h>

void error(int, int, char*);

void error(int val, int ret, char* func_name)
{
        if(val == ret)
        {
                switch(ret)
                {
                        case 0:
                                printf("fatal error at %s, return 0\n", func_name);
                                exit(1);

                        case -1:
                                printf("fatal error at %s, return -1\n", func_name);
                                exit(1);
                }
        }
  return 0;
 }

1 个答案:

答案 0 :(得分:0)

malloc在失败时返回空指针,请参阅here。您的错误函数必须使用指针,因为您无法取消空指针,然后检查它是否为NULL。像这样:

void error(void *ptr, char *func_name)
{
    if(ptr == NULL)
    {     
        printf("fatal error at %s, return NULL\n", func_name);
        exit(1);
    }
}

// call like this:
error(val, "malloc"); // after val = malloc...
error(val->x, "malloc"); // after val->x = malloc...