7

假设我有一个向量类:

typedef struct vec3_s
{
    float x, y, z;
}
vec3;

但是,我希望能够在不将其转换为浮点数组的情况下对其进行迭代。虽然在这种情况下可以接受强制转换,但我很想知道在纯 C 语言中是否可以使用类似 C++ 的功能。例如,在 C++ 中,由于std::vector< T >下标[]运算符重载,我可以传递它的地址函数的第一个索引采用void*.

IE,

void do_something_with_pointer_to_mem( void* mem )
{
    // do stuff
}

int main( void )
{
    std::vector< float > v;

    // fill v with values here

    // pass to do_something_with_pointer_to_mem

    do_some_with_pointer_to_mem( &v[ 0 ] );

    return;
}

另一个更具体的例子是在 OpenGL 中调用glBufferData(...)时(使用 C++ 时):

glBufferData( GL_ARRAY_BUFFER, sizeof( somevector ), &somevector[ 0 ], GL_STREAM_DRAW );

那么,是否可以使用下标运算符在 C 中完成类似的操作?如果不是,并且我必须编写一个函数(例如, ),那么将它放在定义它的头文件中是否float vec3_value_at( unsigned int i )有意义?static inline

4

3 回答 3

26

如果所有结构字段都属于同一类型,则可以使用联合如下:

typedef union vec3_u
{
    struct vec3_s {
        float x, y, z;
    };
    float vect3_a[3];
}
vec3;

这样,您可以独立访问每个 x、y 或 z 字段,或使用 vect3_a 数组对其进行迭代。这个解决方案在内存或计算方面没有任何成本,但我们可能离类似 C++ 的解决方案有点远。

于 2013-01-19T20:49:16.817 回答
4

您没有得到 C++ 的语法糖,但是很容易将您在 C++ 中编写的函数编写为 operator[]。

float get_vec3(v *vec3, int i) {
   switch(i) {
   case 0: return v->x;
   case 1: return v->y;
   case 2: return v->z;
   }
   assert(0);
 }

现在您可以遍历任何 vec3。

 for (int i = 0; i < 3; i++) {
     printf("%f\n", get_vec3(v, i));
 }
于 2013-01-19T20:42:41.550 回答
2

C 中的问题是您需要知道如何在结构中移动(即您需要知道类型)。之所以std::vector<T>如此,是因为它使用了模板(一个C++概念)。现在,也就是说,你可以尝试一些与你建议的稍有不同的东西。如果您不想使用任何数组,则可以存储泛型类型。但是,在检索和使用数据时,用户必须知道他或她期待什么样的数据。下面避免了数组(尽管在使用它们时存在一个潜在的更清洁的解决方案)并且有一个链表实现,它给你几乎相同的灵活性std::vector<T>(除了性能优势,因为这是一个链表O(n)对所有内容的操作(您可以聪明地反转列表来实现,也许,O(1)插入,但这只是示例)

#include <stdio.h>
#include <stdlib.h>
typedef struct _item3_t
{
  void *x, *y, *z;
  struct _item3_t* next;
} item3_t;

typedef struct
{
  item3_t* head;
} vec3_t;

void insert_vec3(vec3_t* vec, void* x, void* y, void* z)
{
  item3_t* item = NULL;
  item3_t* tmp  = NULL;
  int i = 0;
  if(vec == NULL)
    return;

  item = malloc(sizeof(item3_t));
  item->x = x;
  item->y = y;
  item->z = z;
  item->next = NULL;

  tmp = vec->head;
  if(tmp == NULL) { // First element
    vec->head = item;
  } else {
    while(tmp->next != NULL)
      tmp = item->next;
    tmp->next = item;
  }
}

// This is one method which simply relies on the generic method above
void insert_vec3_float(vec3_t* vec, float x, float y, float z)
{
  float* xv, *yv, *zv;
  if(vec == NULL)
    return;
  xv = malloc(sizeof(float));
  yv = malloc(sizeof(float));
  zv = malloc(sizeof(float));

  *xv = x;
  *yv = y;
  *zv = z;

  insert_vec3(vec, xv, yv, zv);
}

void init_vec3(vec3_t* vec)
{
  if(vec == NULL)
    return;
  vec->head = NULL;
}

void destroy_vec3(vec3_t* vec)
{
  item3_t* item = NULL, *next = NULL;
  if(vec == NULL)
    return;

  item = vec->head;
  while(item != NULL) {
    next = item->next;
    free(item->x);
    free(item->y);
    free(item->z);
    free(item);
    item = next;
  }
}

item3_t* vec3_get(vec3_t* vec, int idx)
{
  int i = 0;
  item3_t* item = NULL;
  if(vec == NULL)
    return NULL;
  item = vec->head;
  for(i = 0 ; i < idx && item != NULL ; ++i)
    item = item->next;
  return item;
}

void do_something(item3_t* item)
{
  if(item == NULL)
    return;
  float x = *((float*)item->x);
  float y = *((float*)item->y);
  float z = *((float*)item->z);

  // To do - something? Note, to manipulate the actual
  // values in the vector, you need to modify their values
  // at their mem addresses
}

int main()
{
  vec3_t vector;

  init_vec3(&vector);

  insert_vec3_float(&vector, 1.2, 2.3, 3.4);

  printf("%f %f %f\n", *((float*)vec3_get(&vector, 0)->x), *((float*)vec3_get(&vector, 0)->y), *((float*)vec3_get(&vector, 0)->z));

  do_something(vec3_get(&vector, 0));

  destroy_vec3(&vector);

  return 0;
}

此代码应立即编译。您在这里拥有的是一个链表,它是您的“向量”(特别是 vec3 结构)。列表中的每个节点(即std::vector<T>意义上的每个元素)都有 3 个元素,它们都是void指针。因此,您可以在此处存储您希望的任何数据类型。唯一的问题是您需要为这些指针分配内存以指向并且在删除元素时,您需要释放该内存(请参阅vec3_destroy示例方法)。希望这有助于更多地了解这些 void 指针如何在您的情况下工作。

要检索数据,您将无法使用该[]符号,但您可以vec3_get以相同的方式使用该方法。该do_something方法是某种方式的示例存根,您可能能够完成类似于您在 OP 中提到的内容。

于 2013-01-19T21:15:07.877 回答