我如何填充一个C函数内部结构的全球基准的结构指针? [英] How do I populate a struct pointer with global struct reference inside a C function?

查看:116
本文介绍了我如何填充一个C函数内部结构的全球基准的结构指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C和无法理解为什么my_struct_ptr(主)为零下面的例子。我将如何分配my_structs数组的指针my_struct_ptr的get_my_struct_by_name功能?

内的一个结构的地址

 结构my_struct {
    字符*名称;
    字符* DESCR;
    字符*值;
} my_structs [3] = {
    {一,一个描述,值1},
    {B,B描述,值2},
    {C,C说明,值3}
};INT get_my_struct_by_name(字符*名称,结构my_struct * my_struct_ptr){    INT I;
    对于(I = 0; I&≤(的sizeof(my_structs)/的sizeof(结构my_struct));我++){
            如果(STRCMP(姓名,my_structs [我]。名称)== 0){                    my_struct_ptr =安培; my_structs [I]                    的printf(作品:%S%S%S \\ n,my_struct_ptr->的名字,my_struct_ptr-> DESCR,my_struct_ptr->值);
                    返回0;
            }
    }    返回-1;
}
诠释主(){
    INT RES = 0;
    结构my_struct * my_struct_ptr;    如果(RES = get_my_struct_by_name(B,my_struct_ptr))
            返回水库;    的printf(零:%P \\ N,my_struct_ptr);    的printf(赛格故障:%S%S%S \\ n,my_struct_ptr->的名字,my_struct_ptr-> DESCR,my_struct_ptr->值);    返回水库;
}

编辑:添加输出到例如希望能帮助别人。谢谢大家谁回答。这正是我一直在寻找的帮助!

输出:

  [提示〜] $ ./test
工作原理:B,B的描述,值2
零:(无)
分段故障


解决方案

您必须将指针传递的指针(即通过其地址):

  INT get_my_struct_by_name(字符*名称,结构my_struct ** my_struct_ptr)
{// ^^^
    // ...
    * my_struct_ptr = / * ... * /
}诠释的main()
{
    结构my_struct * my_struct_ptr;
    get_my_struct_by_name(姓名,&安培; my_struct_ptr);
    // ...
}

如果你只是按值传递指针,像你一样,你只修改指针,而不是原来的指针的本地副本。

道德:如果你想要一个函数在呼叫范围内改变一个变量,你必须说&安培; 地方

I am new to C and having trouble understanding why my_struct_ptr (main) is nil in the following example. How would I assign the address of a struct in the my_structs array to the my_struct_ptr pointer within the get_my_struct_by_name function?

struct my_struct {
    char *name;
    char *descr;
    char *value;
} my_structs[3] = {
    {"a", "a description", "value 1"},
    {"b", "b description", "value 2"},
    {"c", "c description", "value 3"}
};

int get_my_struct_by_name(char *name, struct my_struct *my_struct_ptr) {

    int i;
    for (i=0; i < (sizeof(my_structs)/sizeof(struct my_struct)); i++) {
            if (strcmp(name, my_structs[i].name) == 0) {

                    my_struct_ptr = &my_structs[i];

                    printf("works: %s,%s,%s\n", my_struct_ptr->name, my_struct_ptr->descr, my_struct_ptr->value);
                    return 0;
            }
    }

    return -1;
}


int main() {
    int res = 0;
    struct my_struct *my_struct_ptr;

    if (res = get_my_struct_by_name("b", my_struct_ptr))
            return res;

    printf( "nil: %p\n", my_struct_ptr);

    printf("seg fault: %s,%s,%s\n", my_struct_ptr->name, my_struct_ptr->descr, my_struct_ptr->value);

    return res;
}

Edit: Adding example output to hopefully help others. Thank you to everyone who responded. This is exactly the help I was looking for!

Output:

[prompt ~]$ ./test
works: b,b description,value 2
nil: (nil)
Segmentation fault

解决方案

You have to pass the pointer as a pointer (i.e. pass its address):

int get_my_struct_by_name(char *name, struct my_struct **my_struct_ptr)
{                                                   // ^^^
    // ...
    *my_struct_ptr = /* ... */
}

int main()
{
    struct my_struct * my_struct_ptr;
    get_my_struct_by_name(name, &my_struct_ptr);
    // ...
}

If you just pass the pointer by value, as you did, you only modify a local copy of the pointer, not the original pointer.

Moral: If you want a function to change a variable in the calling scope, you have to say & somewhere.

这篇关于我如何填充一个C函数内部结构的全球基准的结构指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆