So I created a dynamically allocated array of structs pretend that structtype is the name of the struct, and arr is the array.
structtype * arr;
arr = new structtype[counter 15];
Then I attempted to pass that array of structs into multiple types of functions with prototypes such as:
void read_info(structtype (&arr)[15], int integer);
and
void Append(structtype arr[15]);
In turn I got these errors:
error: cannot convert ‘structtype’ to ‘structtype (&)[15]’
and
error: cannot convert ‘structtype**’ to ‘structtype (&)[15]'
Can yall help me figure out how to get rid of these errors, for example dereferencing the pointer part of the array or something.
CodePudding user response:
Your array is somewhere in memory, location which is pointed by the actual variable arr
the pointer.
The array is therefore represented by the pointer, however the pointer carries no information about where the array ends or its size, unlike std::vector<>.
So you need to pass it with the known size as in:
#include <cstdio>
struct structtype {
int a;
};
void dosomething( structtype* s, int size ) {
for ( int j=0; j<size; j ) {
printf( "s[%d]=%d\n", j, s[j] );
}
}
int main() {
int counter = 50;
structtype * arr;
arr = new structtype[counter 15];
dosomething( arr, counter 15 );
delete arr;
}
Godbolt: https://godbolt.org/z/95P5d4sfT