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

passing by reference in a variable length parameter list

Status
Not open for further replies.

txjump

Programmer
May 17, 2002
76
US
Hello all,

i have two functions. one trims the right and left sides of a char* array - strTrim(char* str). the other takes a variable length parameter list and calls strTrim w/ each char* array in the list.

bool nullChecker(int numOfStrs, ...){

int i = 0;
va_list marker;
va_start( marker, numOfStrs );
while( i++ < numOfStrs )
{
char* currStr = va_arg(marker, char*);
cout << currStr << endl;
if(currStr == NULL || *currStr == NULL ||
*(currStr = strTrim(currStr)) == NULL)
*currStr = NULL;

cout << &quot;currStr :&quot; << currStr << &quot;:&quot; << endl;
}
va_end( marker );

return true;
}

as long as i call strTrim directly from main, my char* arrays get changed because they are passed by reference. but when i call nullChecker from main, my char* arrays dont get changed ... if i check the arrays after nullChecker, they are the original arrays w/ the spaces.

am i messing up my pointers or is it possible to do what im trying to do?

thanks :)
txjump
 
Hi txjump,

Hmmmm... unusual code (The design) I would say.

Instead of calling this routine with a variable number of arguments, I would erase (!) the nullChecker() and call the strTrim() function the required number of times instead.

Example :

char * str1, * str2, str3;
..... (assignments) ...

Remove THIS : nullChecker(3, str1, str2, str3);
And do like this instead :
strTrim(str1);
strTrim(str2);
strTrim(str3);


Keep it simple !!


/JOlesen


 
well, once i get the variable parameter list part working i will move it all into one function.

the problem is that this is for a dll. the dll is going to be accessed by cobol and from what the cobol programmer is telling me, she cant pass me a null address without some big coding issues. so i have to check every single char* array she passes to me and alter it to what i need for the underlying java code. i have about 40 functions that deal w/ char* arrays that have to call strTrim and each function can have as many as 20 char* arrays. i was trying to make it all in one call.

this dll is a bridge between cobol and java. so reducing the number of function calls would be best. after some testing, the va_list macros just wont let me use a reference. so i may just pass an array of char* pointers and be done w/ it. just didnt want to allocate more memory for the array.

oh well, thanks for the suggestion though. :)
txjump
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top