send int pointer array to a function and return code example
Example 1: how to create and return a struct array in C
fentry *read_fentries(FILE *fp){
fentry *files[MAXFILES];
if ((fread(files, sizeof(fentry), MAXFILES, fp)) == 0) {
fprintf(stderr, "Error: could not read file entries\n");
closefs(fp);
exit(1);
}
return files;
}
Example 2: c++ function return array
#include <iostream>
#include <ctime>
using namespace std;
int * getRandom( ) {
static int r[10];
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i) {
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
int main () {
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}