pointers as parameters and arguements

I'm trying to pass a dynamic array into a function and fill it but i can't figure out how to do it correctly and i can't find any good literature on it either... here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//////////////////////////////////////////////////////////////////////
//   An Excercise in Pointers: Blood Donations!						//
//takes previously generated data file and displays information		//
//in an easy to see and analyze format								//
//Programmer: Tyler Sievers											//
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void getdata(int list, int& count);

void main()
{
	int *Donation_List;
	int *Size;
	Size = new int;
	*Size = 0;
	Donation_List = new int[*Size]; 
	//at this point i have declared my dynamic array
	//as per instructions Size will be a pointer and
	//is currently set to zero as there are no elements 
	//currently in the array size will also end up
	//being the total number of donors when the array
	//is finished being populated
	
	getdata(*Donation_List, *Size);//opens file gets data from file and closes file
	//at this point size is the total number of donors!
	cout<< *Size;

}

void getdata(int list, int &count)
{
	ifstream data;
	data.open("donations.txt");
	if(data.fail())
	{
		cout<< "Failed to open 'donations.txt' please make sure that the file is accessable and try again.";
		exit(1);
	}
	while(!data.fail())
	{
		data >> list[&count];
		count++;
	}
	data.close();
}

When i run it the console just crashes... When i debug the crash comes at the very first data>>list[&count];... I'm wondering if &count is putting the address in the index there and its crashing because it isn't a valid one... I don't know how to fix this though...
I'm wondering if &count is putting the address in the index there and its crashing because it isn't a valid one...

Correct. This is the same as (&count)[list] and obviously doesn't do what you want.

I don't think you understood what dynamic allocations are - Arrays cannot be resized after they've been created, whether they were allocated dynamically or not. Dynamic "arrays" are called vectors in C++ and you can use push_back to add elements to them. See here:
http://www.cplusplus.com/reference/stl/vector/

main() must return int, by the way. void is not allowed.
Last edited on
yeah i know that main is supposed to return int my teacher doesn't though -.-

So vectors are off limits for me and my arrays are to use exactly the right amount of space for the data in the file without actually knowing how much data is in the file. I don't understand how i am to accomplish this? I am sure he wants an array based structure also so what can i do to make a data structure that will scale as data is added from zero to whatever is necessary without making a preliminary guess... I'm not asking for code just some ideas to run with...
You can first count the number of elements in the file and then create an array with exactly the right size.
I feel like that is terribly inefficient but limited from vectors as i am i guess that is what i will have to do... i could write a data structure class but that would take time and i would have to violate rules of encapsulation to do some of the stuff that he is requiring i do... maybe i will look into that a little bit tomorrow... thanks for the help Athar!
Topic archived. No new replies allowed.