如何使变量返回N/a?(c语言)

如何使变量返回N/a?(c语言),c,C,我有一个c语言的代码,还有一个返回int值的函数。但是,在非完整字段条件的情况下,我需要该函数中的一个变量来给出值N/a(不是它在开始时初始化的大数字)。有什么建议吗?非常感谢……C中没有N/a。最接近它的是值指针。您可以让一个函数返回指向int的指针,在未满足条件的情况下返回NULL 基本上有两种可能,返回指向静态存储的指针,或者返回指向使用malloc分配的存储的指针 int *may_fail_static(int input) { static int result; i

我有一个c语言的代码,还有一个返回int值的函数。但是,在非完整字段条件的情况下,我需要该函数中的一个变量来给出值N/a(不是它在开始时初始化的大数字)。有什么建议吗?非常感谢……

C中没有N/a。最接近它的是值指针。您可以让一个函数返回指向int的指针,在未满足条件的情况下返回NULL

基本上有两种可能,返回指向静态存储的指针,或者返回指向使用malloc分配的存储的指针

int *may_fail_static(int input) {
    static int result;
    if (input == 42)
         return NULL;
    else {
        result = 3 * input;
        return &result;
    }        
}

int *may_fail_malloc(int input) {
    int *result;
    if (input == 42)
        return NULL;
    else {
        result = malloc(sizeof *result);
        if (result == NULL) {
            fprintf(stderr, "Out of memory!\n");
            exit(1);
        }
        *result = 3 * input;
        return result;
    }
}
两者都有缺点:静态版本不可重入(非线程安全)。马洛克酒店 版本有很大的分配开销,客户端必须在使用后显式释放存储

这就是为什么在C中,您通常会发现这类函数:

/* Returns 0 on success, or -1 on failure */
int may_fail(int input, int *result) {
    if (input == 42)
        return -1;
    else {
        *result = 3 * input;
        return 0;
    }
}
客户可通过以下方式使用它:

int x;
if (may_fail(42, &x) == -1) {
    fprintf(stderr, "ERROR: may_fail failed!\n");
    exit(1);
}
/* Answer value is now stored in x. Go ahead */

我同意乔的看法。您可以做的另一件事是在函数中将errno(3)设置为某个有意义的值,然后在函数外部检查它的值。

有几种方法

1.使用代理值 在这样的情况下,最简单的方法是为N/a保留值,该值作为正常返回值无效。常见的例子是许多库函数,它们返回读/写/某物的字节数,或者返回-1表示错误。如果有效值的范围已知,则在此范围之外定义一些合适的值,表示不适用

#include <limits.h>
// define NA_VALUE to be smallest possible int
#define NA_VALUE (INT_MIN)

int getInt1(const char *key) {
    if (test_if_key_exits(key)) {
        return get_int_for_key(key);
    }
    else {
        return NA_VALUE;
    }
}

void testfunc1(const char *key) {
    int value = getInt1(key);
    if (value != NA_VALUE) std::cout << value;
    else std::cout << "n/a";
}
您还可以将其反转,将int作为返回值返回,并将out参数用于status。如果您希望始终返回某个值,即使是默认值(如果没有其他值),并且允许out参数指示它是默认值,那么这可能非常有用

3.返回指向值的指针,或
NULL

第三种方法是返回一个指向值的指针,或者NA的
NULL
,但您询问的是
int
返回值,因此这可能不实用,存在一些缺点。在可以返回指向某些现有数据的指针的情况下,它非常有用。这个替代方案及其缺点在中进行了解释。

我在C库中看到了四种使用方法:

/*
 * return a defined unexpected value, like -1,
 * otherwise the return the result
 *
 * return -1 if failed, otherwise on success
 */
int my_function(void) {
  return -1;
}




N/A的值应该是多少?AFAIK
int
只能是数值,如果输入超过某个值,则返回null?您可以使用带符号-1表示
无符号int
NaN
表示浮点数,
null
表示指针。@d在这种情况下,我倾向于使用相反的顺序(即,让状态为返回值)。另一个选项可能是使用
struct retval{int value;char success;}
,然后定义
struct retval函数(void)
。我不确定篡改
errno
是否是一个好主意-这应该保留给运行库。这是一个解决方案。是的,这是一个糟糕的解决方案。也许像其他人建议的那样改变设计会更好。+1:代理值(也称为sentinel值)是对我自己的帖子的一个很好的补充。@JoSo谢谢,我想这是我想要的词,添加到了答案中。
/*
 * return a defined unexpected value, like -1,
 * otherwise the return the result
 *
 * return -1 if failed, otherwise on success
 */
int my_function(void) {
  return -1;
}
/*
 * the result is stored in `ret`, and the state
 * is returned
 *
 * return -1 if failed, otherwise on success
 */
int my_function(int *ret) {
  return -1;
}
/*
 * the state is stored in an argument
 *
 * state = -1 if failed, otherwise on success
 */
int my_function(int *state) {
  *state = -1;
  return 0;
}
/*
 * with pointers and addresses
 *
 * return NULL on failed, otherwise on success
 */
int *my_function(void) {
  return NULL;
}