简体   繁体   English

将结构指针数组传递给c中的函数

[英]passing array of strucct pointers to function in c

I am trying to pass a array of pointers to structs to a function. 我正在尝试将指针数组传递给函数。 then assign the address of that array to a pointer and use that pointer to index the elements in the array. 然后将该数组的地址分配给一个指针,并使用该指针为数组中的元素建立索引。

below I created three instances of a struct and assign the struct values then I create a array of pointers and have the elements in the array point to these structs. 下面,我创建了一个结构的三个实例并分配了结构值,然后创建了一个指针数组,并使数组中的元素指向这些结构。

I then pass the array of structs to a functions that prints its the value of the structs in two different ways... 然后,我将结构体数组传递给一个函数,该函数以两种不同的方式输出其结构体的值...

The first way is I index the array that I pass to the function and print out the values in the individual structs.... this works fine! 第一种方法是索引传递给函数的数组,并打印出各个结构中的值。

The second way is I assign the array to a pointer and I index this pointer to print out the values in the array... this doesn't work correctly 第二种方法是将数组分配给一个指针,并为该指针编制索引以打印出数组中的值...这无法正常工作

struct name_struct_pointer {
    char *name1;
    char *name2;
};
int main(void) {


struct name_struct_pointer name_struct_pointer_inst_1 = {"name one","name two"};
struct name_struct_pointer name_struct_pointer_inst_2 = {"name three","name four"};
struct name_struct_pointer name_struct_pointer_inst_3 = {"name five","name six"};

 struct name_struct_pointer * name_struct_pointer_array[3] = {
        &name_struct_pointer_inst_1,
        &name_struct_pointer_inst_2,
        &name_struct_pointer_inst_3
};

        print_array_of_name_struct(name_struct_pointer_array,3);
        return EXIT_SUCCESS;
}

void print_array_of_name_struct(struct name_struct_pointer * name_struct_pointer_array[],int size){

    puts("print data indexed by function arg");
        for(int i = 0; i < size;i++){
            printf("print_name_struct : %s : %s \r\n",name_struct_pointer_array[i]->name1,name_struct_pointer_array[i]->name2);
        }

    struct name_struct_pointer * local_name_struct_pointer_array = name_struct_pointer_array;
    puts("print data indexed by local pointer");
    for(int i = 0; i < size;i++){
        printf("print_name_struct : %s : %s \r\n",local_name_struct_pointer_array[i].name1,local_name_struct_pointer_array[i].name2);
    }
}

I expected the output of the pointer indexing method to give the same results, but that didn't happened. 我希望指针索引方法的输出能给出相同的结果,但是那没有发生。 I got a mangled incorrect version of my data. 我的数据版本错乱了。

print data indexed by function arg 打印由函数arg索引的数据
print_name_struct : name one : name two print_name_struct:名称一:名称二
print_name_struct : name three : name four print_name_struct:名称三:名称四
print_name_struct : name five : name six print_name_struct:名称五:名称六
print data indexed by local pointer 打印由本地指针索引的数据
print_name_struct : dP@ : vP@ print_name_struct:dP @:vP @
print_name_struct : ïP@ : name five print_name_struct:ïP@:名称五
print_name_struct : name six : name three print_name_struct:名称六:名称三

I suspect there is something going on when assign the array to the pointer. 我怀疑将数组分配给指针时发生了什么事情。 it was my understanding that if I assign the array to a pointer I could use pointer arithmetic to index the data in the array. 据我了解,如果我将数组分配给指针,则可以使用指针算法来索引数组中的数据。 What am I not understanding? 我不明白什么?

WAY 1: The *local_name_struct_pointer_array should be declared as a double pointer **local_name_struct_pointer_array , because you are assigning incompatible pointer size. 方式1:应该将*local_name_struct_pointer_array声明为双指针**local_name_struct_pointer_array ,因为您正在分配不兼容的指针大小。

Then, you should also change how you access the members of the struct from pointer[i].member to pointer[i]->member . 然后,您还应该将如何将pointer[i].member的结构从pointer[i].memberpointer[i]->member

WAY 2: Other way should be by keeping how you wrote the code, and only change the pointer assignment as this: *local_name_struct_pointer_array = name_struct_pointer_array[0] , so you are getting the first element of the pointer array, and then you loop over the elements. 方式2:另一种方式应该是保持代码的编写方式,并且仅按以下方式更改指针分配: *local_name_struct_pointer_array = name_struct_pointer_array[0] ,因此您正在获取指针数组的第一个元素,然后遍历元素。

one of the example to why one should not neglect the warning, especially involving the pointers. 为什么不应该忽略警告的示例之一,尤其是涉及指针。 The code gives 'incompatible pointer type' warning. 该代码给出“不兼容的指针类型”警告。

Solution is declare as double pointer 解决方案被声明为双指针

Changes required 需要变更

struct name_struct_pointer ** local_name_struct_pointer_array =name_struct_pointer_array;

and

printf("print_name_struct : %s : %s \r\n",local_name_struct_pointer_array[i]->name1,local_name_struct_pointer_array[i]->name2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM