Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access to structure members

Status
Not open for further replies.

wswain

Technical User
Nov 1, 2003
21
US
Hello,

I am trying to create a program that has an array of structures. To visualize, the structures would look like an excel spreadsheet of records. The indexing I need to do has to go from top to bottom, i.e. struct.member, plus go across with a method to index on the member. I can not use a 2-d array as the data types are mixed. That would have made a 2d indexing easy.

So far I've tried to create a variable that would index and then become a string, but is not accepted by the compiler as a valid meber identifier. The members of a structure record are basically some basic data plus 52 fields for the weeks of the year that I need to sequence across week1, week2, week3, etc. I need to do this for each record to do do some analytical functions based on time for each record and then put the result into that week of the year's "field" or struct.member.

My instinct tells me I am probably doing this all wrong, but.....it is seems logical that there is a database type routine to move around the 2d structure of records automatically by indexing.

Any assistance would be greatly received.

Thanks,
Bill


 
perhaps you could use an array of pointers
to deal with the mixed data types

maybe, could be
I'll shut up now. ;-)

tomcruz.net
 
If I understand you correctly, it's just a matter of different data types for each record in the database, right?

So you create a structure with a union:

struct StcMyCell
{
int iWeekNumber;
int iValueType;
union
{
char cValue;
int iValue;
double dValue;
bool bValue;
}
};

... then define the database:

struct StcMyCell myDataBase[MaxRows][MaxCols];

The size of each member cell will depend on the largest member of union (in my case it's double - 8 bytes if I remember it correctly) as they share the same memory place. So take big care about it :)

oh.. and btw, iValueType is used for determining which of the union's values is the correct one to use, so don't forget to set some constant value to it everytime you set something in union.

When you do it, do it right.
 
wswain,
If I have understood you correctly , you need a linked list.
Code:
typedef struct myTag
{
.....data
......

struct myTag *pRight;
struct myTag *pLeft;
struct myTag *pTop;
struct myTag *pBottom;



}MyStrcut;


You can join them up as you like(by setting the pointers of the last element to the newly created one ) and allocate memory for them on the fly (when you create a new struct).


If this is what you want , can give you more details if you need them.
 
Thanks everybody.

I think the structure with a union of DeVooXiam's posting looks like it will get me the 2 d mixed array. I get very confused about the pointers array, linked list as to me that seems more indicative of a parent child type database linkage.

After structuring the union and setting the database it looks like I've got a 2d indexabale array. Currently my struct looks like the following. I have thought that I'm really creating a memory block allocation problem, but in fact each element onced processed upon will have data in it.

typedef struct USAGEMATRIX {
char Serialnum[10];
char Servstat[2];
char holdplace1[5];
.
.
.
double wk1;
double wk2;
double wk3;
double wk4;
double wk5;
double wk6;
double wk7;
double wk8;
.
.
.
.
double wk52


} series;


Then the program processes code on each wk# element that is an a algorithm to put a number into the cell. The recuring algorithm has to see previous index'd elements of the record to arrive at the value for active element.

I hope this adds more details and thanks for the assistance.

Bill
 
In addition, whouldn't it be easier to process and visually nicer to have an array of double's for the wk# members? :)
i.e. double wk[52];

------------------
When you do it, do it right.
 
How do I link each row of the matrix to the record structure for all the other pertanaint data in the record for the item. The combined effect is a record history. In database format the parent child link works great, but the math we need to do isn't supported well except by 'C' code for a couple of reason.

And, yes we want to output a complete datbase for reporting once all the calcs are done.

Thanks again,
Bill
 
Please disregard my last email. It did not dawn on me about a matrix contained within in a structure i.e. series.wk, essentially a connected child matrix.

I am now onto the best part, making the darn funtions put the correct formulated values into the array.

Simplicity is beauty

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top