Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的关联数组_C_Arrays_Associative Array_Dongle - Fatal编程技术网

C语言中的关联数组

C语言中的关联数组,c,arrays,associative-array,dongle,C,Arrays,Associative Array,Dongle,我正在实现一种将一组数据传输到可编程加密狗的方法。加密狗基于智能卡技术,可以在内部执行任意代码。输入和输出数据作为二进制块传递,可通过输入和输出指针访问 我想使用关联数组来简化数据处理代码。一切都应该这样: 首先,主机应用程序: // Host application in C++ in_data["method"] = "calc_r"; in_data["id"] = 12; in_data["loc_a"] = 56.19; in_data["loc_l"] = 44.02; proces

我正在实现一种将一组数据传输到可编程加密狗的方法。加密狗基于智能卡技术,可以在内部执行任意代码。输入和输出数据作为二进制块传递,可通过输入和输出指针访问

我想使用关联数组来简化数据处理代码。一切都应该这样:

首先,主机应用程序:

// Host application in C++
in_data["method"] = "calc_r";
in_data["id"] = 12;
in_data["loc_a"] = 56.19;
in_data["loc_l"] = 44.02;
processor->send(in_data);
接下来是加密狗内的代码:

// Some dongle function in C
char* method_name = assoc_get_string(in_data, "method");
int id = assoc_get_int(in_data, "id");
float loc_a = assoc_get_float(in_data, "loc_a");
float loc_l = assoc_get_float(in_data, "loc_l");
所以我的问题是关于加密狗部分的功能。是否有C代码或库来实现上述关联数组行为?

实现映射接口或(关联数组)。 它很可能是C语言中使用最多的哈希表实现

GHashTable *table=g_hash_table_new(g_str_hash, g_str_equal);

/* put */
g_hash_table_insert(table,"SOME_KEY","SOME_VALUE");

/* get */
gchar *value = (gchar *) g_hash_table_lookup(table,"SOME_KEY");

油嘴滑舌,可能就是你想要的。

是的,但它不会以你指定的方式工作。它将使用一个
struct
来存储在该结构上运行的数据和函数,从而得到所需的结果。看见使用示例:

struct map_t *test;

test=map_create();
map_set(test,"One","Won");
map_set(test,"Two","Too");
map_set(test,"Four","Fore");

我怀疑你必须自己写。如果我理解您所描述的体系结构,那么您将需要在单个数据块中发送整个数据块。如果是这样的话,那么大多数库将无法实现这一点,因为它们很可能会分配多块内存,这将需要多次传输(以及对结构的内部理解)。这类似于尝试使用库哈希函数,然后通过套接字通过网络发送其内容,只需将根指针传递给
send
函数


可以编写一些自己的实用程序,在单个内存块中管理非常简单的关联数组(或散列)。如果数据量很小,则可以使用简单的线性搜索来搜索条目,这将是一段相当紧凑的代码。

Try,一个在C中实现哈希表的头库。它很小,而且非常容易使用。

Mark Wilkins给了您正确的答案。如果您想将数据作为一个单独的块发送,您需要了解C++架构如何在体系结构中表示并编写访问函数。 无论如何,如果您决定在加密狗上重新创建地图,我已经编写了一个小的C库,您可以在其中编写如下代码:

tbl_t in_data=NULL;

tblSetSS(in_data,"method","calc_r");
tblSetSN(in_data,"id",12);
tblSetSF(in_data,"loc_a",56.19);
tblSetSF(in_data,"loc_l",44.02);
然后:

char  *method_name = tblGetP(in_data, "method");
int    id          = tblGetN(in_data, "id");
float  loc_a       = tblGetF(in_data, "loc_a");
float  loc_l       = tblGetF(in_data, "loc_l");
hashtable是hopsocch散列的一个变体,它平均来说相当好,并且您可以对键和数据使用任何类型的混合(即,您可以使用整个表作为键)

该函数的重点是简化编程,而不是纯粹的速度,并且代码没有经过彻底测试,但是如果您喜欢这个想法,并希望在其上进行扩展,您可以查看上面的代码


(还有其他东西,比如可变长度字符串和快速字符串模式匹配函数,但在本例中可能不感兴趣)。

这是一个旧线程,但我认为这对任何正在寻找实现的人来说可能仍然有用。它不需要太多的代码;我花了大约100行的时间完成了我的工作,没有任何额外的图书馆。我称它为字典,因为它与python数据类型(某种程度上)相似。这是我的密码:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct hollow_list hollow_list;

struct hollow_list{
    unsigned int size;
    void *value;
    bool *written;
    hollow_list *children;
};

//Creates a hollow list and allocates all of the needed memory
hollow_list hollow_list_create(unsigned int size){
    hollow_list output;
    output = (hollow_list) {.size = size, .value = (void *) 0, .written = calloc(size, sizeof(bool)), .children = calloc(size, sizeof(hollow_list))};
    return output;
}

//Frees all memory of associated with a hollow list and its children
void hollow_list_free(hollow_list *l, bool free_values){
    int i;
    for(i = 0; i < l->size; i++){
        hollow_list_free(l->children + i, free_values);
    }
    if(free_values){
        free(l->value);
    }
    free(l);
}

//Reads from the hollow list and returns a pointer to the item's data
void *hollow_list_read(hollow_list *l, unsigned int index){
    if(index == 0){
        return l->value;
    }
    unsigned int bit_checker;
    bit_checker = 1<<(l->size - 1);
    int i;
    for(i = 0; i < l->size; i++){
        if(bit_checker & index){
            if(l->written[i] == true){
                return hollow_list_read(l->children + i, bit_checker ^ index);
            } else {
                return (void *) 0;
            }
        }
        bit_checker >>= 1;
    }
}

//Writes to the hollow list, allocating memory only as it needs
void hollow_list_write(hollow_list *l, unsigned int index, void *value){
    if(index == 0){
        l->value = value;
    } else {
        unsigned int bit_checker;
        bit_checker = 1<<(l->size - 1);
        int i;
        for(i = 0; i < l->size; i++){
            if(bit_checker & index){
                if(!l->written[i]){
                    l->children[i] = hollow_list_create(l->size - i - 1);
                    l->written[i] = true;
                }
                hollow_list_write(l->children + i, bit_checker ^ index, value);
                break;
            }
            bit_checker >>= 1;
        }
    }
}

typedef struct dictionary dictionary;

struct dictionary{
    void *value;
    hollow_list *child;
};

dictionary dictionary_create(){
    dictionary output;
    output.child = malloc(sizeof(hollow_list));
    *output.child = hollow_list_create(8);
    output.value = (void *) 0;
    return output;
}

void dictionary_write(dictionary *dict, char *index, unsigned int strlen, void *value){
    void *hollow_list_value;
    dictionary *new_dict;
    int i;
    for(i = 0; i < strlen; i++){
        hollow_list_value = hollow_list_read(dict->child, (int) index[i]);
        if(hollow_list_value == (void *) 0){
            new_dict = malloc(sizeof(dictionary));
            *new_dict = dictionary_create();
            hollow_list_write(dict->child, (int) index[i], new_dict);
            dict = new_dict;
        } else {
            dict = (dictionary *) hollow_list_value;
        }
    }
    dict->value = value;
}

void *dictionary_read(dictionary *dict, char *index, unsigned int strlen){
    void *hollow_list_value;
    dictionary *new_dict;
    int i;
    for(i = 0; i < strlen; i++){
        hollow_list_value = hollow_list_read(dict->child, (int) index[i]);
        if(hollow_list_value == (void *) 0){
            return hollow_list_value;
        } else {
            dict = (dictionary *) hollow_list_value;
        }
    }
    return dict->value;
}

int main(){
    char index0[] = "hello, this is a test";
    char index1[] = "hello, this is also a test";
    char index2[] = "hello world";
    char index3[] = "hi there!";
    char index4[] = "this is something";
    char index5[] = "hi there";

    int item0 = 0;
    int item1 = 1;
    int item2 = 2;
    int item3 = 3;
    int item4 = 4;

    dictionary d;
    d = dictionary_create();
    dictionary_write(&d, index0, 21, &item0);
    dictionary_write(&d, index1, 26, &item1);
    dictionary_write(&d, index2, 11, &item2);
    dictionary_write(&d, index3, 13, &item3);
    dictionary_write(&d, index4, 17, &item4);

    printf("%d\n", *((int *) dictionary_read(&d, index0, 21)));
    printf("%d\n", *((int *) dictionary_read(&d, index1, 26)));
    printf("%d\n", *((int *) dictionary_read(&d, index2, 11)));
    printf("%d\n", *((int *) dictionary_read(&d, index3, 13)));
    printf("%d\n", *((int *) dictionary_read(&d, index4, 17)));
    printf("%d\n", ((int) dictionary_read(&d, index5, 8)));
}
#包括
#包括
#包括
typedef结构空心列表空心列表;
结构空表{
无符号整数大小;
无效*值;
书面的;
空表*儿童;
};
//创建一个空列表并分配所有需要的内存
空心列表空心列表创建(无符号整数大小){
空表输出;
输出=(空心列表){.size=size、.value=(void*)0、.write=calloc(size,sizeof(bool)),.children=calloc(size,sizeof(空心列表))};
返回输出;
}
//释放与空列表及其子项关联的所有内存
void hollow_list_free(hollow_list*l,bool free_值){
int i;
对于(i=0;isize;i++){
空心列表\u free(l->children+i,free\u值);
}
if(自由_值){
自由(l->价值);
}
免费(l);
}
//从空心列表中读取并返回指向该项数据的指针
void*hollow\u list\u read(hollow\u list*l,无符号整数索引){
如果(索引==0){
返回l->value;
}
无符号整数位校验器;
位检测器=1大小;i++){
if(位检查和索引){
如果(l->writed[i]==true){
返回空列表读取(l->children+i,位检查程序^index);
}否则{
返回(void*)0;
}
}
位检测器>>=1;
}
}
//写入空列表,仅根据需要分配内存
void hollow_list_write(hollow_list*l,无符号整数索引,void*值){
如果(索引==0){
l->value=value;
}否则{
无符号整数位校验器;
位检测器=1大小;i++){
if(位检查和索引){
如果(!l->写入[i]){
l->children[i]=空心列表\u创建(l->size-i-1);
l->writed[i]=真;
}
空心列表写入(l->children+i,位检查程序^索引,值);
打破
}
位检测器>>=1;
}
}
}
typedef结构字典;
结构字典{
无效*值;
空表*子项;
};
字典字典创建(){
字典输出;
output.child=malloc(sizeof(空心列表));
*output.child=hollow\u list\u create(8);
output.value=(void*)0;
返回输出;
}
void dictionary\u write(dictionary*dict,char*index,unsigned int strlen,void*value){
空*空列表值;
新词典;
int i;
对于(i=0;ichild,(int)index[i]);
如果(空心列表值==(空心*)0){
new_dict=malloc(sizeof(dictionary));
*new_dict=dictionary_create();
空列表写入(dict->child,(int)索引[i],新dict);
dict=新的dict;
}否则{
dict=(字典*)空心