Re: Tcl C pass by reference



Hi Derek,
You work too hard <g> - and you still think too much in terms of C
coding. Look, in Tcl it is absolutely easy to return multiple values:
Just return a list.

If I understood your requirements correctly I would do it like this:

int fred(int *major,int *minor)
{
*major=1;
*minor=2;
return; // what did you intend to return ????
}

/* Tcl Obj command that provides a tcl script interface to C function
*/
int fredTcl(ClientData clientdata, Tcl_Interp *interp, int objc,
Tcl_Obj *CONST objv[])
{
int major, minor;

// prepare a list
Tcl_Obj *lstPtr = Tcl_NewListObj( 0, NULL);

// get the result from 'fred'
fred( &major, &minor);

// stuff results into a Tcl list
Tcl_ListObjAppendElement( interp, lstPtr, Tcl_NewIntObj(major));
Tcl_ListObjAppendElement( interp, lstPtr, Tcl_NewIntObj(minor));

// make this list the result of the command
Tcl_SetObjResult(interp, lstPtr);

// everything is all right
return TCL_OK;
}

Now from Tcl you just do
foreach {major minor} [fred] {break}
and your (Tcl) variables major and minor will hold fred's result.

This is sort of a "generic" use of Tcl_NewListObj. In your case you
can shorten the code still a bit more by assigning in Tcl_NewListObj
the first list value (left as an exercise for the reader <g>).
HTH
Helmut Giese
.