[gameprogrammer] Re: passing pointer to array
- From: Andy Hynes <andy@xxxxxxxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Mon, 29 Oct 2007 19:37:23 +0000
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
Christoph Harder wrote:
Hello,
I'm currently writing a mergesort for school (yes i know nothing
directly related to gamedev but maybe you can help) and got a bit
confused.
I have a pointer (in the main-function): int *numbers = 0;
a function for reading int values from a file:
int fileToArray(std::string filename, int *numbers, int &len);
in the function i allocate memory for the numbers:
numbers = new int[len];
I want to use both the length and the numbers in the main function,
but when I try to print them i get either an exeption, or the memory
adresse:
for(int i = 0; i < len; ++i){
std::cout<<numbers[i]; //exeption
//std::cout<<&numbers[i]; //strange output (propably the adresses)
}
This is how i call the function:
if(fileToArray(filelist[fi], numbers, len))
{...}
Can anybody please tell me what I have done wrong?
Thanks in advance
Christoph
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: passing pointer to array
- From: Christoph Harder
- References:
- [gameprogrammer] passing pointer to array
- From: Christoph Harder
Other related posts:
- » [gameprogrammer] passing pointer to array
- » [gameprogrammer] Re: passing pointer to array
- » [gameprogrammer] Re: passing pointer to array
- » [gameprogrammer] Re: passing pointer to array
- » [gameprogrammer] Re: passing pointer to array
Hello,I'm currently writing a mergesort for school (yes i know nothing directly related to gamedev but maybe you can help) and got a bit confused.
I have a pointer (in the main-function): int *numbers = 0; a function for reading int values from a file: int fileToArray(std::string filename, int *numbers, int &len); in the function i allocate memory for the numbers: numbers = new int[len]; I want to use both the length and the numbers in the main function,but when I try to print them i get either an exeption, or the memory adresse:
for(int i = 0; i < len; ++i){
std::cout<<numbers[i]; //exeption
//std::cout<<&numbers[i]; //strange output (propably the adresses)
}
This is how i call the function:
if(fileToArray(filelist[fi], numbers, len))
{...}
Can anybody please tell me what I have done wrong?
Thanks in advance
Christoph
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- [gameprogrammer] Re: passing pointer to array
- From: Christoph Harder
- [gameprogrammer] passing pointer to array
- From: Christoph Harder