单指针指向在C两个不同的常量结构表(查找表) [英] Single Pointer pointing to two different const struct table(look up tables) in c

查看:117
本文介绍了单指针指向在C两个不同的常量结构表(查找表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的结构和两个常量查询表如下

 的typedef const的结构
{
    unsigned int类型NUM;
    无符号字符名称[100];
    无符号整型值1;
    无符号整型值2;
    无符号整型值3;
} st_Table1;的typedef const的结构
{
    unsigned int类型NUM;
    无符号字符名称[100];
} st_Table2;st_Table1 stTable1 [] =
{
    {1,名称1,12,13,14},
    {2,名称2,22,23,24},
    {3,NAME3,32,33,34},
    {4,NAME4,42,43,44}
};st_Table2 stTable2 [] =
    {1,India1},
    {2,India2},
    {3,India3}
};

但有可能有一个指针可以指向两个查找表 stTable1 stTable2 <? / p>

当我不得不作出的任何两个表的选择,我们可以分配表的地址的决定(之一)。

但在那之后,我想使用单一指针在剩下的code。

请任何逻辑答复... ...一丝线索

阿文德


解决方案

您可以得到相同的效果,其他的石膏已经与工会建议,以及

 工会table_ptr {
   st_Table1 *表1;
   st_Table2 *表2;
} my_table_ptr;

和刚分配/进入需要的成员。

I have two different structures and and two const look up tables as below

typedef const struct 
{
    unsigned int  num;
    unsigned char name[100];
    unsigned int value1;
    unsigned int value2;
    unsigned int value3;
}st_Table1;

typedef const struct
{
    unsigned int  num;
    unsigned char name[100];
}st_Table2;

st_Table1 stTable1[] =
{
    { 1, "Name1", 12, 13, 14 },
    { 2, "Name2", 22, 23, 24 },
    { 3, "Name3", 32, 33, 34 },
    { 4, "Name4", 42, 43, 44 }
};



st_Table2 stTable2[] =
    { 1, "India1" },
    { 2, "India2" },
    { 3, "India3" }
};

Could it be possible to have single pointer that can point to both the lookup tables stTable1 and stTable2?

When I have to make the decision for selection of either of the two tables we can assign the address of the table (either of).

But after that I wanted to use the single pointer in the remaining code.

Please reply for any logic ... hint ... clue

Arvind

解决方案

You can get the same effect as the casts other have suggested with a union as well

union table_ptr {
   st_Table1*table1;
   st_Table2*table2;
} my_table_ptr;

and just assign/access the desired member.

这篇关于单指针指向在C两个不同的常量结构表(查找表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆