[gameprogrammer] Re: passing pointer to array

Thank you very much.
Works perfectly, hopefully I will understand it completly. (I should propably read a few more pointer tutorials or something).


Andy Hynes wrote:
I think your program is confused regarding pointers and pointers to pointers. I think you need something like the following:

int fileToArray(stf::string filename, int **numbers, int* len) {
 /* ... */
 num_integers = ... /* read number of integers from the file */
 *numbers = (int*)malloc(sizeof(int) * num_integers);
 *len = num_integers;
 /* read the integers from the file */
}

main() {
 int len;
 int *numbers;
 if (fileToArray(filename, &numbers, &len)) {
   for(int i = 0; i < len; ++i){
     std::cout<<numbers[i];
   }
 }
}

Note different prototype for fileToArray.

All the best,

Andy

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: