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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem storing values being passed between functions 1

Status
Not open for further replies.

jamez05

Programmer
Jul 29, 2005
130
0
0
US
Hi,

I'm using visual studio 2005 on windows.

I'm creating a struct that I'm trying to pass between
functions. I set it up in a header file:
Code:
typedef struct DataElement2 {
	char Sub[75];
	int Pos;
} Substituent;

I initialize it in my main function :
Code:
SP = new DataElement2 [ 75 ];
I want to pass this to another function where it gets populated. The function's name is ev_num. This function
is called multiple times. Leaving other parts out for
simplicity:

main function call:
Code:
loop {
ev_num(SP);
}
ev_num:
Code:
int ev_num(Substituent *SP) {
 // figured out SubPosCounter in some code above (leaving out - know its correct)
 SP[SubPosCounter].Pos = t->p[branchid];
 SubPosCounter++;
}
When I print out SP at the end of my main function, its
missing values that were found in the individual calls. Not
sure what I'm doing wrong, seems like SP gets erased each
time I call ev_num.

Any help would be greatly appreciated.
 
Well, you have structure type with two (redundant;) names: DataElements2 and Substituent (what for?).
Then you allocate array[75] of this type structures and have a pointer to it (SP - all capitals names denotes macro constant traditionally).
Now you pass it to the ev_num function and modify the array with (global?) indicies.
That's OK.
No errors on that level.
Check SubPosCounter values...
 
Thanks ArkM,

It had to do with the SubPosCounter. I'm kind of new to C++,
the redunt names is used because I have 4 other containers that use the dataelements2 structure: substituent, parent,
etc.. Didn't know of a different way.
 
About redundancy:
In C++ (but not in C) you may declare:
Code:
struct MyType
{
...
};
// ...and now:
MyType mystruct;
MyType* mystructptr;
// ... and so on. MyType in struct is this struct type name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top