简体   繁体   English

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

[英]How do I pass a structure as an argument to a function for error checking in C?

General description of code:- 代码的一般描述:-

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.

Environment: Linux, OS: fedora 环境:Linux,操作系统:fedora

error 错误

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 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 input function 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 Error Handler header file 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;
 }

malloc returns a null pointer on failure, see here . 如果失败, malloc返回一个空指针,请参见此处 Your error function has to take a pointer, as you can't derefernce a null pointer, and then check whether it is NULL . 您的错误函数必须使用一个指针,因为您不能取消引用空指针,然后检查它是否为NULL Something like this: 像这样:

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...

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

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