c - 在函数调用期间避免 sizeof 样板的宏函数

标签 c function macros type-safety variadic

我声明了一个具有以下签名的函数(简化了实现):

#include <stdio.h>

struct test_s{
    int a, b;
};

void foo(struct test_s **out, size_t *szs, size_t arr_len){
    for(size_t i = 0; i < arr_len; i++){
        for(size_t j = 0; j < szs[i]; j++){
            struct test_s ts = out[i][j];
            printf("a = %d; b = %d\n", ts.a, ts.b);
        }
    }
}

如果调用者使用数组调整为指针,则可以按如下方式调用:

int main(void){
    struct test_s a1[] = {{0, 1}, {2, 3}, {4, 5}};
    struct test_s a2[] = {{4, 6}};
    foo((struct test_s *[]){a1, a2}, 
        (size_t[]){sizeof a1 / sizeof(struct test_s), sizeof a2 / sizeof(struct test_s)}, 
        2);
}

可以看出,函数调用看起来复杂、容易出错且难以阅读。

当谈到使用 3 个参数时,情况变得更糟:

int main(void){
    struct test_s a1[] = {{0, 1}, {2, 3}, {4, 5}};
    struct test_s a2[] = {{4, 6}};
    struct test_s a3[] = {{2, 3}, {4, 5}};
    foo((struct test_s *[]){a1, a2, a3},
        (size_t[]){sizeof a1 / sizeof(struct test_s), sizeof a2 / sizeof(struct test_s), sizeof a3 / sizeof(struct test_s)},
        3);
}

所以当涉及到数组时,将其实现为宏是完美的。按如下方式实现它非常简单:

#define FOO_ARR_2(a1, a2) \
    do{ \
        foo((struct test_s *[]){a1, a2},  \
        (size_t[]){sizeof a1 / sizeof(struct test_s), sizeof a2 / sizeof(struct test_s)}, \
        2);\
    } while(0)

我看到这样的宏有 2 个问题:

  1. 我必须定义 FOO_ARR_3 , FOO_ARR_4等等……
  2. 缺乏类型安全。如果来电者传递了不同的东西,那么 struct test_s[]

问题: 是否可以将其实现为可变宏函数,如 #define FOO_ARR(...)

最佳答案

与其将复杂的初始化包装到复杂的(如果可能的话)可变参数宏中,不如让事情变得更加复杂,只需将相关函数声明为可变参数本身即可。

这可能看起来像这样:

#include <stdio.h>
#include <stdarg.h>

/* expects: number-of-arrays followed by 
   number-of-arrays tuples {arrays-size,  pointer to array's 1st element} */

struct test_s{
  int a, b;
};

void foo(size_t arr_len, ...)
{
  va_list vl;

  va_start(vl, arr_len);

  for (size_t i = 0; i < arr_len; ++i) 
  {
    size_t s = va_arg(vl, size_t);
    struct test_s * p = va_arg(vl, struct test_s *);

    for (size_t j = 0; j < s; ++j)
    {
      struct test_s ts = p[j];
      printf("a = %d; b = %d\n", ts.a, ts.b);
    }
  }

  va_end(vl);
}

像这样使用它:

struct test_s{
  int a, b;
};

void foo(size_t, ...);

int main(void)
{
  /* using two arrays: */
  {
    struct test_s a1[] = {{0, 1}, {2, 3}, {4, 5}};
    struct test_s a2[] = {{4, 6}};

    foo(2, 
      sizeof a1 / sizeof *a1, a1, 
      sizeof a2 / sizeof *a2, a2
    );
  }

  /* using three arrays: */
  {
    struct test_s a1[] = {{0, 1}, {2, 3}, {4, 5}};
    struct test_s a2[] = {{4, 6}};
    struct test_s a3[] = {{2, 3}, {4, 5}};

    foo(3, 
      sizeof a1 / sizeof *a1, a1, 
      sizeof a2 / sizeof *a2, a2,
      sizeof a3 / sizeof *a3, a3
    );
  }
}

关于c - 在函数调用期间避免 sizeof 样板的宏函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619266/

相关文章:

c - 枚举数据类型内存分配

javascript - 单击 ul > li 仅在第一次有效

c++ - Qt 5 中的宏 "QT_BEGIN_NAMESPACE"是什么意思?

c - 在 C 中移动二维数组中的列

c - 需要以有效方式确定 C 中数组大小的建议

c - 多CPU作业的用户时间增加

c++ - 有没有一种检查 C++ 中常见子字符串的好方法?

c++ - 如何将数组传递给函数,以便在更改该函数中的数组值时原始数组的值不会改变?

c - C 中的宏数据类型声明

clojure - 如何在线程 (->) 宏中键入提示?