c - 使用存储在其结构中的变量访问数组元素

标签 c arrays struct hashtable lookup-tables

typedef struct Foo Foo;

struct Foo{
 int bar;
 int var2; //etc...
};

Foo array[1000];

int main(){
    int var;
    while(var < 1000)
    {
        array[var].bar = //an unique number
        var++;
    }
}

假设我有这样的代码。在数组的每个元素中,都有一个具有唯一编号的变量 (bar)。现在,我想使用这个数字来访问整个元素(并且可能操纵它的其他变量)。我该怎么做?

最佳答案

In each element of the array, there is a variable with an unique number (bar).

这很容易实现,因为您可以控制您的唯一 key 。
如果您安排数组的键按排序顺序排列,则可以使用 C 库函数 bsearch 进行快速查找。

(注意:请在您的代码中初始化int var;。)

Now I want to use this number to access to the entire element (and maybe manipulate it's other variables)

这可以像为函数 get_foo 指定一个键一样简单:

Foo *obj =  get_foo(3);

简单的例子:

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

#define ARRAY_SIZE 5
typedef struct Foo Foo;

struct Foo{
    int bar;  // key
    int var2; // etc...
    int var3; // etc...
};

Foo array[ARRAY_SIZE];

static int compare_keys(const void *va, const void *vb) {
    const Foo *a = va, *b = vb;
    return (a->bar -  b->bar);
}

Foo * get_foo(int key) {
    Foo *foo = bsearch(&key, array, sizeof array / sizeof array[0],
        sizeof array[0], compare_keys);
    return foo; 
}

int main(void){
    int var = 0;

    while(var < ARRAY_SIZE)
    {
        array[var].bar =    var;     // an unique key number 
        array[var].var2 = 2*var;     // some data
        array[var].var3 = 3*var;     // some data
        var++;
    }

    // The array of structures should be sorted by the value of the unique key 
    // verification that we can look up all the valid keys.
    for (int i = 0; i < sizeof array / sizeof array[0]; ++i) {
        Foo * v = get_foo(array[i].bar);

        if(v)
            printf("key: %3d ->  values:  %2d,  %3d\n", array[i].bar, v->var2, v->var3 );
        else
           printf("Key not found %2d \n", array[i].bar); 
    } 

    // finding a particular object is simple:
    printf("\nFind object for key = %2d \n", 3); 
    Foo *obj =  get_foo(3);
    if(obj)
        printf("key: %3d ->  values:  %2d,  %3d\n", 3, obj->var2, obj->var3 );

    return 0;
}

测试:

key:   0 ->  values:   0,    0                                                                                                                 
key:   1 ->  values:   2,    3                                                                                                                 
key:   2 ->  values:   4,    6                                                                                                                 
key:   3 ->  values:   6,    9                                                                                                                 
key:   4 ->  values:   8,   12                                                                                                                 

Find object for key =  3                                                                                                                       
key:   3 ->  values:   6,    9   

关于c - 使用存储在其结构中的变量访问数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49328939/

相关文章:

c - 这是在堆上优化多维数组的可能方法吗?

java - ParseJson.java,如果没有数组名称则解析结果?

java - 为什么我的 Java 程序只在文件中添加一半的数字列表?

c - 结构、类型定义和 C 头文件;如何在没有 .c 文件的情况下集成

c - Qsort 字符串结构

c - 叔叔和侄子之间的管道

c - 如何从结构中打印指针的值

struct - 调用带 `self`的方法时不修改数据结构

c - 字符串比较不起作用 C

javascript - React js 中的数组重新排列