我有5个结构模式" module tbtop;
virtual top_if intf();
...
assign intf.init_sig = `BLOCK_PATH.INIT_SIG;
endmodule
interface top_if;
logic init_sig;
endinterface
// where, `BLOCK_PATH is defined in separate define file like this:
`define BLOCK_PATH $root.tbtop.RTL.init_block
// where tbtop is testbench top module in systemverilog
// where RTL is instance of RTL module in tbtop
// where init_block is instance sub-module in rtl from which INIT_SIG is tapped
// INIT_SIG is declared as output of std_logic type, it is an output connected to another sub-module within RTL.
"所有人都共享相同数量和类型的字段。
例如
struct_ID_name
当我通过struct struct_01
{
char field1[25];
int field2;
}
struct struct_01 struct_01_name;
struct struct_02
{
char field1[25];
int field2;
}
struct struct_02 struct_02_name;
时,是否可以编写函数,我可以访问该特定结构的变量吗?
例如
char ID[2]
为了给出一些上下文,我有不同的记录,前两个字段是共同的。这两个字段告诉我记录的大小,记录的名称....每种记录类型中的字段数是不同的。每个记录的结构名称是模式struct__name,其中ID可以是任何两位数字。我想检索公共字段。
更多信息: 有一个程序正在解析文件中的记录。
文件可以有50种类型的记录。 每条记录包含不同数量的字段。
所有这50条记录中都有一些共同的字段。 每条记录中的第一个字段告诉我记录的大小, 第二个字段告诉我记录的一些uniq键 第三个字段告诉我一些不同的东西。
我需要解析strucutres / records中的公共字段。
所以我想到了一些通用的东西来实现这一点,一旦我通过ID,我应该能够从记录中恢复大小。
答案 0 :(得分:1)
你的要求很奇怪,你可以这样做:
void foo(char * ID)
{
if (strcmp(ID, "01"))
{
int i = struct_01_name.field2;
printf("%d", i);
}
if (strcmp(ID, "02"))
{
int i = struct_02_name.field2;
printf("%d", i);
}
else if (strcmp(ID, "03"))
{
int i = struct_03_name.field2;
printf("%d", i);
}
...
}
但我认为你的问题是XY Problem。 您应该告诉我们有关您的用例的更多信息。
答案 1 :(得分:1)
您可以使用宏和##
运算符。
##
运算符用于连接。
#include<stdio.h>
//The macro
#define X(num) \
i=struct_##num##_name.field2; \
printf("\nstruct_%s_name.field2 = %d", #num, i);
//The structures
struct struct_01
{
char field1[25];
int field2;
};
struct struct_01 struct_01_name;
struct struct_02
{
char field1[25];
int field2;
};
struct struct_02 struct_02_name;
//main() function
int main()
{
int i;
struct_01_name.field2=10;
struct_02_name.field2=20;
X(01)
X(02)
}
##
在i=struct_##num##_name.field2;
中的作用是,给予宏的参数num
介于'struct_'和'_name'之间。
##
连接令牌和'is known as the "Token Pasting operator"'
请在维基百科页面上阅读C preprocessor。
这将产生以下输出:
struct_01_name.field2 = 10
struct_02_name.field2 = 20
即使你评论的有50种此类结构,你也可以使用它。
行:
X(01)
X(02)
将被预处理器替换为:
i=struct_01_name.field2; \
printf("\nstruct_%s_name.field2 = %d", "01", i);
i=struct_02_name.field2; \
printf("\nstruct_%s_name.field2 = %d", "02", i);