使用存储在其结构中的变量访问数组元素
Access to an array element using a variable stored in it's structure
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)
.
这很容易实现,因为您可以控制您的唯一密钥。
如果您安排数组的键按排序顺序排列,则可以使用 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
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)
.
这很容易实现,因为您可以控制您的唯一密钥。
如果您安排数组的键按排序顺序排列,则可以使用 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