在C中通过引用传递结构,但修改不';不全在那里

在C中通过引用传递结构,但修改不';不全在那里,c,arrays,struct,pass-by-reference,configuration-files,C,Arrays,Struct,Pass By Reference,Configuration Files,我试图制作一个简单的程序来读取C中的配置文件。但是我的结构成员中的数据并没有停留在我放的地方 代码在底部 这就是我认为我正在做的: 我正在创建一个 通过引用传递结构 从配置文件填充结构的成员 当我试图访问成员时,我只能从配置文件的最后一行获取数据 谢谢 编辑:我希望它用配置文件中的键/值填充结构。相反,它只有配置文件中最后一行的值 预期产出 example.com 实际产出 food.txt 问题在于: conf->key[i] = key; conf->value[i]

我试图制作一个简单的程序来读取C中的配置文件。但是我的结构成员中的数据并没有停留在我放的地方

代码在底部

这就是我认为我正在做的:

  • 我正在创建一个
  • 通过引用传递结构
  • 从配置文件填充结构的成员
当我试图访问成员时,我只能从配置文件的最后一行获取数据

谢谢


编辑:我希望它用配置文件中的键/值填充结构。相反,它只有配置文件中最后一行的值

预期产出

example.com

实际产出

food.txt

问题在于:

conf->key[i]   = key;
conf->value[i] = value;
main.c

/*
* Goal is to make simple key value storage with struct and pointers
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 100
#define CONFIG_MAX_BUF 1024
#define DELIM "="

struct config_s {
    char *key[MAXLEN];
    char *value[MAXLEN];
};

void read_config_file(struct config_s *conf, char *filename, int *size_of_array);

int main() {
    int i = 0, size_of_array;
    // type config_s has key and value members
    struct config_s c;

    read_config_file(&c, "test.cfg", &size_of_array);
    printf("size of array: %i\n", size_of_array);

    // if i want to print them all out, i can use this, but i'll have 
    // a function to search for a certain key and return the value.
    int l = 0, e = 1;
    printf("********** TEST 1 ***********\n");
    printf("key: [%s] value: [%s] i: [%i]\n", c.key[l], c.value[l], l);
    printf("key: [%s] value: [%s] i: [%i]\n", c.key[e], c.value[e], e);
    printf("********** TEST 2 ***********\n");

    return 0;
}

void read_config_file(struct config_s *conf, char *filename, int *size_of_array) {
    FILE *file;
    file = fopen(filename, "r");

    char line[CONFIG_MAX_BUF];

    int i = 0;

    char *cfline;
    char *key;
    char *value;

    while(fgets(line, sizeof(line), file) != NULL) {
        cfline = strstr((char *)line, DELIM);
        value = cfline + strlen(DELIM);
        value[strlen(value) - 1] = '\0';

        key = strtok((char *)line, DELIM);
        printf("%i %s %s\n", i, key, value);
        conf->key[i]   = key;
        conf->value[i] = value;
        printf("%i %s %s\n", i, (*conf).key[i], (*conf).value[i]);
        i++;
    }
    printf("%s %s\n", (*conf).key[0], (*conf).value[0]);
    fclose(file);
    *size_of_array = i;
    return;
}

您的代码中可能存在多个问题。我可以在
get\u value\u by\u key
函数中看到其中一个。这里,您正在将
conf->key[i]
key
进行比较,这永远不会是真的(因为您正在比较两个不同的指针)。相反,您要做的是:

if(strncmp(conf->key[i], key, strlen(conf->key[i])+1) == 0) 
比较这两个指针指向的两个字符串

char *get_value_by_key(config_s *conf, char *key, int config_size) {
    int i;
    for(i=0;i<config_size;i++) {
        /*if(conf->key[i] == key) {*/ //Instead of this
        /* Do this */
         if(strncmp(conf->key[i], key, strlen(conf->key[i])+1) == 0) {
            return conf->value[i];
         }
    }
    /* Also return NULL if there is no match */
    return NULL;
}
char*get\u value\u by_key(config\u s*conf,char*key,int-config\u size){
int i;
对于(i=0;ikey[i]==key){*///而不是这个
/*这样做*/
如果(strncmp(conf->key[i],key,strlen(conf->key[i])+1)==0){
返回conf->value[i];
}
}
/*如果没有匹配项,也返回NULL*/
返回NULL;
}

在最新版本的
read_config_file
中,您正在保存指向堆栈上缓冲区
内的值的指针,因此这些指针在返回时将无效

即使缓冲区是全局的,每行的指针也会相互冲突,因为缓冲区会被每次
fgets
调用覆盖

更改:

conf->key[i]   = key;
conf->value[i] = value;
进入:


这是因为您分配的是指针,而不是字符串。
在C
中通过引用传递结构,但在C中没有通过引用传递机制!C只有传递值机制,而不是通过引用传递,因为C不支持这一点。您正在传递指向结构的指针。使用恰当的术语很重要。@CherubimAnand:严格地说,是的。另一方面,可以说您可以使用指针在C中实现(相当于)按引用传递(pass-by-reference)。类似地,C没有链表,但可以使用结构和指针实现它们;没有人会说这样的实现不是一个真正的链表。谢谢!我还在学习,不客气!我已经用C写了[相当长的]一段时间了,但我仍然每天至少创建一个bug。我们都还在学习;-)
conf->key[i]   = key;
conf->value[i] = value;
conf->key[i]   = strdup(key);
conf->value[i] = strdup(value);