排列在不同的结构 [英] Array over different structs
问题描述
说我有两个不同的结构:
Say that I have two different structures:
typedef struct {
int a;
int b;
int c;
int d;
} struct_1;
typedef struct {
int a;
int b;
int e;
int f;
int g;
int h;
} struct_2;
和他们正在以类似的方式用于在两个不同的算法。我会尝试做的是替代两种结构,它们基本上是不同的类型,有DINAMIC数组,使用两个枚举了我实际需要的情况下。其目的将是preserve结构域的型动物名称,而不是使用数字。因此,像:
And that they're used in a similar way in two different algorithm. What I would try to do is to substitute both structures, which are basically different types, with a dinamic array, and using two enums for the cases I actually need. The purpose would be to preserve the differents name of the structure fields, instead of using numbers. So something like:
typedef enum {
a,
b,
c,
d,
num1_fields
} struct_1_fields;
typedef enum {
a,
b,
e,
f,
g,
h
num1_fields
} struct_2_fields;
int *structure;
if(case_1) {
structure = malloc(4*sizeof(int));
} else if(case_2) {
structure = malloc(6*sizeof(int));
} else {
//something else
}
不过,由于我会再次提名相同的枚举器,编译器会无法正常工作......有没有解决这个问题的方法?
However since I would renominate the same enumerator, the compiler wouldn't work... is there a way to solve this problem?
推荐答案
您可以使用基于C OOP方式:
You can use OOP approach on C:
#include <stdio.h>
#include <stdlib.h>
// forward declaration for virtual function table
struct vtable;
// base class
typedef struct {
struct vtable* tbl_;
} base;
// virtual function table
typedef struct vtable {
void(*method_for_algorithm_1_)(base* object);
void(*method_for_algorithm_2_)(base* object);
} vtable;
// algorithm 1 knowns only about base
void algorithm_1(base* p[], int size) {
for (int i = 0; i < size; i++) {
p[i]->tbl_->method_for_algorithm_1_(p[i]);
}
}
// algorithm 2 knowns only about base
void algorithm_2(base* p[], int size) {
for (int i = 0; i < size; i++) {
p[i]->tbl_->method_for_algorithm_2_(p[i]);
}
}
// struct1 is base
typedef struct {
base super_;
int a;
int b;
int c;
} struct1;
// struct2 is base
typedef struct {
base super_;
int a;
int b;
int c;
int d;
int e;
} struct2;
void struct1_method_for_algorithm1(base* object) {
struct1* s1 = (struct1*)object;
printf("struct1_method_for_algorithm1: %d %d %d\n", s1->a, s1->b, s1->c);
}
void struct1_method_for_algorithm2(base* object) {
struct1* s1 = (struct1*)object;
printf("struct1_method_for_algorithm2: %d %d %d\n", s1->a, s1->b, s1->c);
}
void struct2_method_for_algorithm1(base* object) {
struct2* s2 = (struct2*)object;
printf("struct2_method_for_algorithm1: %d %d %d %d %d\n", s2->a, s2->b, s2->c, s2->d, s2->e);
}
void struct2_method_for_algorithm2(base* object) {
struct2* s2 = (struct2*)object;
printf("struct2_method_for_algorithm2: %d %d %d %d %d\n", s2->a, s2->b, s2->c, s2->d, s2->e);
}
int main() {
{
vtable struct1vtable = {
&struct1_method_for_algorithm1,
&struct1_method_for_algorithm2
};
struct1 a[] = {
{ &struct1vtable, 10, 20, 30 },
{ &struct1vtable, 40, 50, 60 },
};
base* p[] = { &a[0], &a[1] };
algorithm_1(p, 2);
algorithm_2(p, 2);
}
{
vtable struct2vtable = {
&struct2_method_for_algorithm1,
&struct2_method_for_algorithm2
};
struct2 a[] = {
{ &struct2vtable, 10, 20, 30, 40, 50 },
{ &struct2vtable, 40, 50, 60, 70, 80 },
};
base* p[] = { &a[0], &a[1] };
algorithm_1(p, 2);
algorithm_2(p, 2);
}
return 0;
}
这篇关于排列在不同的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!