I get an error when i try to compile this code. I tried to allocate memory in main function and that works. But why it doesn't work in function? I think that there is something wrong with function argument, but not sure.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct Word
{
char letter[ 50 ];
};
void ReadFromFile( const char* filename, Word* dictionary, int& n )
{
ifstream fin( filename );
fin >> n;
dictionary = new Word[ n ];
for( int i=0; i<n; i++ )
{
fin >> dictionary[ i ].letter ;
}
fin.close();
}
int main()
{
Word* dictionary;
int n;
ReadFromFile( "A.txt", dictionary, n );
for( int i=0; i<n; i++ )
{
cout << dictionary[ i ].letter << endl;
}
return 0;
}