我是 C 语言新手。我在学习时正在构建一个非常小的“数据库”。我了解 C 语言的大部分基础知识,但我在指针和数组方面遇到了麻烦。
我想创建一个简单的键值查找函数。程序执行时,密钥由用户作为参数 (argv) 输入。
我被困在打印行的特定动态列。我想做这样的事情:
myArray[i][key] // the key is dynamic, i is from a loop
这是查找功能:
void Find(char *key, char *value){
// Print intention to terminal
printf("\nGet where %s=%s\n", key, value);
// Get rows
struct Address *rows = conn->db->rows;
// List rows
int i = 0;
for(i = 0; i < 100; i++){
if(rows[i].set){
printf("rows[%d] = %s\n", i, rows[i][key] ); // <-- how to do this?
}
}
}
这是地址结构:
struct Address {
int id;
int set;
char name[512];
char email[512];
};
这是数据库结构:
struct Database {
struct Address rows[100];
};
// we have to return void*, because we don't know what type to expect
void* getField(struct Address* addr, char* field) {
if(strcmp(field, "name") == 0) return &addr->name;
if(strcmp(field, "id") == 0) return &addr->id;
if(strcmp(field, "email") == 0) return &addr->email;
return 0;
}
但这并没有你希望的那么有帮助,因为:
char* key = "name";
void* value = getField(&row, key);
// uh oh - we don't know what type `value` points to. Better double check:
// This changes per field V V And so does this
if(value == &row.name) printf("row.%s = %s\n", key, *(char(*)[512])value);
if(value == &row.email) printf("row.%s = %s\n", key, *(char(*)[512])value);
if(value == &row.id) printf("row.%s = %d\n", key, *(int*) value);
// We could conceivably write a function to return the correct format specifier, but
// the language does not allow us to write a function to do the appropriate cast
我想创建一个简单的键值查找函数...
和
我被困在打印一行的特定动态列...
1)使用结构体数组作为数据库
2) 将结构体的成员视为行(或记录)的字段
3) 结构体数组应该可以使用 key-value 方法进行搜索
4) 可执行文件作用于用户输入的键值。
dynamic这个词的含义/上下文,在评论中,您表示有兴趣在不使用 if/else 或其他分支方法进行搜索的情况下获取所需的信息。
但是你正在使用一个结构体。我同意结构体在这里是一个不错的选择,因为它容纳多种数据类型(在本例中为整数和字符),而使用二维数组仅限于一种数据类型。因为您表示有兴趣使用结构体数组作为记录集合(关联值的行)的容器,所以在某些时候搜索和操作数据需要使用分支。
下面的代码插图可能会遗漏您所描述的一些要点,但它确实包含一种搜索
数据库(结构体数组,在代码中模拟)的方法,以及:
打印出一列数据,使用
void PrintSelectedColumnData(int col);
或
使用 检索单个标签值对
void GetSelectedColumnData(int col, int row, char *data);
代码 exmaple:包括两个搜索函数(可执行文件需要 0 - 3 的参数)
typedef struct {
int id;
int set;
char name[512];
char email[512];
}ADDRESS;
// column names ID SET NAME EMAIL
ADDRESS Address[] = {{1, 1, "Joe", "[email protected]"}, //for illustration purposes
{2, 2, "Ann", "[email protected]"}, //initialize a struct "database
{3, 3, "Ben", "[email protected]"}, //with fillers in code.
{4, 4, "Gil", "[email protected]"}, //(Can later modify to read from a file)
{5, 5, "pat", "[email protected]"},
{6, 6, "Gin", "[email protected]"},
{7, 7, "Jen", "[email protected]"},
{8, 8, "Wil", "[email protected]"},
{9, 9, "Abe", "[email protected]"},
{10,10, "Del", "[email protected]"}};
enum {
ID,
SET,
NAME,
EMAIL,
MAX_COLS
};
//prototypes
void PrintSelectedColumnData(int col);
void GetSelectedColumnData(int col, int row, char *data) ;
int main(int argv, char *argc[])
{
int col=0;
int i;
char data[512] = {0};
if(argv == 1)
{
printf("no arguments, leaving");
return 0;
}
col = atoi(argc[1]);
PrintSelectedColumnData(col);
GetSelectedColumnData(col, 5, data);//Example: get sixth record of column data
//call with changed arguments to retrieve different data
switch(col) {
case ID:
Address[5].id = atoi(data);
break;
case SET:
Address[5].id = atoi(data);
break;
case NAME:
strcpy(Address[5].name, data);
break;
case EMAIL:
strcpy(Address[5].email, data);
break;
default:
printf("%d is not valid, must used values 0 - 3\n", col);
break;
}
getchar();//stop execution to view resuls
return 0;
}
void PrintSelectedColumnData(int col)
{
int i=0;
for(i=0;i<sizeof(Address)/sizeof(Address[0]);i++)
{
switch(col) {
case ID:
printf("ID %d: %d\n", i+1, Address[i].id);
break;
case SET:
printf("SET %d: %d\n", i+1, Address[i].set);
break;
case NAME:
printf("NAME %d: %s\n", i+1, Address[i].name);
break;
case EMAIL:
printf("ID %d: %s\n", i+1, Address[i].email);
break;
default:
printf("%d is not valid, must used values 0 - 3\n", col);
i = sizeof(Address)/sizeof(Address[0]) + 1;
break;
}
}
}
void GetSelectedColumnData(int col, int row, char *data)
{
switch(col) {
case ID:
sprintf(data, "%d", Address[row].id);
break;
case SET:
sprintf(data, "%d", Address[row].set);
break;
case NAME:
strcpy(data, Address[row].name);
break;
case EMAIL:
strcpy(data, Address[row].email);
break;
default:
printf("%d is not valid, must used values 0 - 3\n", col);
break;
}
}