Serving the Quantitative Finance Community

 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

July 25th, 2002, 12:47 am

Hi, I am a junior member trying to learn C by myself. I know my question might sound dumb but please give me some pointers.How should I allocate a dynamic array of structures in C? Is it possible to do it without malloc if for now my only objective is to determine the number of elements (here employees).eg. I am trying to read a text file that has the following info: Name Age Salarybut I don't know the number of employees.How can I read it into an array of employee structure without using a sentinel?Thanks!
 
User avatar
mholm

simple C question

July 25th, 2002, 11:54 am

Why not use a link list.
 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

July 25th, 2002, 2:59 pm

Thank you.Could you please elaborate a little more on how to use Linked List here??I don't know much about Data Structures yet but am about to start on it. Any advice will be greatly appreciated. Thanks!
 
User avatar
Onuk

simple C question

July 25th, 2002, 3:20 pm

BullDog >> How should I allocate a dynamic array of structures in C? Is it possible to do it without malloc Alas C doesn't allow dynamic sizing of arrays without direct (malloc/calloc) allocation. You could either use a linked list or similar (=malloc/calloc), use a fixed size array which you have reason to believe will be big enough and note the usage size, or go to C++/STL and use vector et al. from STL (the best thing probably).Must rush.
 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

July 25th, 2002, 6:57 pm

Onuk, Thanks. I suppose, then I'll have to go the malloc way for now until I learn C++. Can you give me a simple example of using malloc while trying to read data from a text file and storing it in a structure?
 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

July 25th, 2002, 6:58 pm

Type - should be:storing it in an ARRAY of structure.------------------------------------------------------------------------Onuk, Thanks. I suppose, then I'll have to go the malloc way for now until I learn C++. Can you give me a simple example of using malloc while trying to read data from a text file and storing it in a structure?
 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

July 25th, 2002, 7:13 pm

TYPO - not type :-)store data inARRAY Of STRUCTURE
 
User avatar
dfaussie
Posts: 0
Joined: July 14th, 2002, 3:00 am

simple C question

August 1st, 2002, 9:04 am

If at all possible use C++ as it comes with STL ( Standard Template Library ). If this is possible you can do something like:#include <stdio.h>#include <string>using namespace std;typedef struct{ string strName; int nAge; float fpSalary;} NameAgeSalary;vector< NameAgeSalary > vNAS;FILE *pF = fopen( "data.txt", "r" );if ( pF ){ while ( fgets( sz, sizeof( sz ) - 1, pF ) ) { char szName[1024]; int nAge = 0; float fpSalary =0; int n = sscanf( sz, "%[A-Za-z .] %[0-9] %[0-9]", szName, &nAge, &fpSalary ); if ( n == 3 ) { NameAgeSalary nas; nas.strName = szName; nas.nAge = nAge; nas.fpSalary = fpSalary; vNAS.push_back( nas ); } } fclose( pF );}You might want to use a map<> or something else instead of a vector as you will undoubtely want to do some indexing, etc. You might want to use iostreams instead of fgets and sscanf but only if you want your app to run really slow or if there isn't much data. You can probably find lots of code for this on the web. With the STL you don't have to worry about the memory allocation as STL takes care of it. Then you can pass a reference or a pointer to your vector ( or a copy if you want to make things really slow ) when you call functions, etc. You could also do the following if you really wanted to do the memory allocation yourself:vector< NameAgeSalary *> vNAS; NameAgeSalary *pnas = new NameAgeSalary; // or ( NameAgeSalary *)malloc( sizeof( NameAgeSalary ) );, this won't work as long as NameAgeSalary.strName is a string if ( pnas ) { pnas->strName = szName; pnas->nAge = nAge; pnas->fpSalary = fpSalary; vNAS.push_back( pnas ); }The variations are endless. Good luck.
 
User avatar
bulldog
Topic Author
Posts: 0
Joined: June 2nd, 2002, 4:30 am

simple C question

August 5th, 2002, 8:47 pm

dfaussie,Thanks a ton! your solution is really helpful. I had gone down the path of linked list as suggested by another member and this is also a great alternative. As I said, I don't have formal programming background but am enjoying this ride - with all the invaluable help - Junior Member
 
User avatar
dfaussie
Posts: 0
Joined: July 14th, 2002, 3:00 am

simple C question

August 5th, 2002, 10:42 pm

Good luck with it. Let me know if you get any compiler errors or anything that you can't work out. I would be glad to try to help.