Understanding the dynamic creation of an array of structure and access to its elements

I need to pass the address of a pointer to the structure of a function that inturn will dynamically allocate memory for an array of structures and fill in the values.

Now from my call method, as soon as I get back from func1, I should be able to iterate over the structure array and display the value of the structure variables.

Can someone explain how to pass the address of a pointer to a structure, also iterating through an array of structures created dynamically?

my sample code is as follows:

struct test { int a; int b; }; void func1(int *n,struct test **testobj) { n=5; *testobj = (struct test*) malloc(n*sizeof(struct test)); for(i=0;i<n;i++) { (*testobj)[i].a=1; (*testobj)[i].b=2; } } int main() { struct test testobj;int n; func1(&n,&testobj); for(i=0;i<n;i++) { printf("%d %d",(*testobj)[i].a,*testobj)[i].b); } free(testobj); } 
+4
source share
4 answers

In main (), define a pointer to the test structure:

 struct test *testPtr; 

To take the address of this pointer, use the & address-of operator:

 &testPtr; 

This returns the address of the pointer and is of type struct test **

You can then pass this into your func1 function, which makes the correct allocation (although casting malloc() usually considered bad practice - Am I taking the result of malloc? ). Other than that, func1() looks good ... line ...

 *testobj = malloc(n*sizeof(struct test)); 

... right. *testobj your double pointer, which you do by doing &testPtr , and stores the new memory address in your pointer. You are also right when you look up your double pointer with (*testobj)[i] because [] has a higher priority than * which you need (as you did) to surround the dereferencing with brackets to make sure this will happen before you index.

Thus, when func1() returns a pointer to testPtr , it should now point to an array of n test structures that you have allocated, and they can be accessed using testPtr[i].a , etc.

EDIT: your for loop should become

 for(i=0;i<n;i++) printf("%d %d", testobj[i].a, testobj[i].b); 

Was your source loop supposed to give you compilation errors? testobj not a pointer in the source code, so dereferencing it should not be possible.

So, the final answer is in main() declare testobj as a pointer, and then accesses the elements of the array as testobj[n] :)

EDIT: As Eric pointed out, remove n=5; from func1() . I think you meant *n=5 , perhaps as some kind of debugging step ... You probably want to use n as an input to the function to indicate how many objects you want in your structured array. Either initialize n , or perhaps override func1() as

 void func1(int n,struct test **testobj) // n is no longer a poitner, just a number 
+3
source

create your array of pointers to structures in the declaration step itself and just pass it to the function

 struct test *testobj[10]; func1(&n,testobj); 

This passes the entire array of function pointers

0
source

Your code looks very good to me. only editing that should do it well -

instead

 struct test testobj; 

enter the following code

 struct test *testobj; 

and save the rest as it is ..!

here the working version of what is required, here the memory is allocated in the called function in the same way as required

 #include <stdlib.h> #include <stdio.h> struct tests { int a; int b; }; void func1(int *n,struct tests **testobj) { int i; *n=5; *testobj = (struct tests*) malloc((*n)*sizeof(struct tests)); for(i=0;i<(*n);i++) { (*testobj)[i].a=1; (*testobj)[i].b=2; } } int main() { int i; struct tests *testobj;int n; func1(&n,&testobj); for(i=0;i<(n);i++) { printf("%d %d",(testobj)[i].a,testobj[i].b); } free(testobj); } 
0
source

It is not clear which version you are asking for, but one of them should cover it:

 /* allocate some number of tests. * * out_n: out parameter with array count * returns: an array of tests */ struct test* allocate_some_tests(int *out_n) { int n = 5; /* hardcoded, random or otherwise unknown to caller */ *out_n = n struct test *t = malloc(n * sizeof(*t)); while (n--) { t[n].a = 1; t[n].b = 2; } return t; } /* allocate a specific number of tests. * * n: in parameter with desired array count * returns: an array of tests */ struct test* allocate_n_tests(int n) { struct test *t = malloc(n * sizeof(*t)); while (n--) { t[n].a = 1; t[n].b = 2; } return t; } 

Please note that you can simply return the selected array, here you do not need a pointer to a pointer.

Both for calling them and for iterating the result:

 void print_tests(struct test *t, int n) { for (; n--; t++) printf("{%d, %d}\n", t->a, t->b); } int main() { int count1; /* I don't know how many yet */ struct test *array1 = allocate_some_tests(&count1); print_tests(array1, count1); int count2 = 3; /* I choose the number */ struct test *array2 = allocate_n_tests(count2); print_tests(array2, count2); } 
0
source

Source: https://habr.com/ru/post/1488079/


All Articles