[英]C pass values in dynamically increased arrays with pointers to pointers
我正在编写一个C程序,其中我必须执行一个未知数量的结果搜索,将结果放入某种结构中,然后从程序的不同位置访问该结构。
在我看来,这就像C句柄对象(指向指针的指针)可以正常工作的地方。 我给出了希望程序访问的结构,即不变的指针,以便程序的任何部分都可以获取它。 我在某种数组上有一个稳定的句柄,该数组将在程序执行过程中进行处理,并且其地址将随着所保存的数据的增长或收缩而改变。
C具有您所需要做的全部工作:可以指向其他指针的指针; 动态分配内存以扩展对象。 但是:要在C语言中执行此操作,您必须知道如何使用* **&与方括号()[]以及->和句点(。)结合使用。 例如,您必须知道*(place + j)与place [j],(* place)[j]和*(* place + j)相同,以及place-> thing的确切方式, place.thing和(* place)-> thing不同。 等等。 因此,尽管我已经写了30年的C程序,但花了我好几个小时才能搞定。
这是一个可行的解决方案的代码。 我有一个包含两个句柄的process_book_inf结构,它指向我要访问的数据。 我希望这些句柄之一访问整数search_els_found的数组,该数组可以是任意长度。 我希望其他人访问一个指向bounds_obj结构的指针数组,在该结构中可能再有许多这样的结构。 两个函数testInt和testPtr创建两个这样的数组,随着数组的增长创建指向该数组的新指针,然后将新数组的地址放入主process_book结构中。
这是我的问题:我的代码有效,但是我不确定它是否像它一样优雅,简单和透明。 有很多人比我更懂C。 有人有主意吗? (附带说明:此示例演示了C的强大功能)。
#include <stdio.h>
#include <stdlib.h>
struct process_book_inf {
void *search_els_found;
void **search_texts_found;
};
typedef struct process_book_inf *process_book_ptr;
struct bounds_obj {
int start_el;
int start_off;
};
typedef struct bounds_obj *bounds_ptr;
void testInt(void **resultArray);
void testPtr(void **resultArray);
int main(int argc, const char * argv[]) {
process_book_ptr testMemory=malloc(sizeof(struct process_book_inf));
testInt((void **) &testMemory->search_els_found);
for (int c=0; c<5; c++) {
printf("retrieved %d\r", *((int *) (testMemory->search_els_found)+c));
}
testPtr((void **) &testMemory->search_texts_found);
for (int c=0; c<5; c++) {
bounds_ptr *dest= (bounds_ptr *) (testMemory->search_texts_found)+c;
printf("retrieved structure values %d %d\r", (*dest)->start_el, (*dest)->start_off);
}
return 0;
}
void testInt(void **testarray) {
int *place=(int *) malloc(sizeof(int));
int **already=(int **) testarray;
for (int c=0; c<5; c++) {
if (c>0) {
free(place);
place=(int *) malloc((c+1) *sizeof(int));
for (int j=0; j<c; j++){
place[j]=(*already)[j];
// *(place+j)=*(*already+j);
}
}
*(place+c)=c*100+c;
printf("set %d\r", *(place+c));
*already=place;
}
}
//in this version, simplify as far as I can
//replace *(place+j) by place[j]; remove all redundant casts
void testPtr(void **textarray) {
bounds_ptr *place= malloc(sizeof(bounds_ptr));
bounds_ptr **already=(bounds_ptr **) textarray;
for (int c=0; c<5; c++) {
if (c>0) {
free(place);
place= malloc(sizeof(bounds_ptr)*(c+1));
for (int j=0; j<c; j++){
place[j]= (*already)[j];
}
}
struct bounds_obj *dest= malloc(sizeof(struct bounds_obj));
dest->start_el=(c*100)+1;
dest->start_off=(c*100)+2;
place[c]=dest;
printf("set structure values %d %d\r", place[c]->start_el, place[c]->start_off);
*already= place;
}
}
结果是:
set 0
set 101
set 202
set 303
set 404
retrieved 0
retrieved 101
retrieved 202
retrieved 303
retrieved 404
set structure values 1 2
set structure values 101 102
set structure values 201 202
set structure values 301 302
set structure values 401 402
retrieved structure values 1 2
retrieved structure values 101 102
retrieved structure values 201 202
retrieved structure values 301 302
retrieved structure values 401 402
Program ended with exit code: 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.