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)
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>).
Helmut Giese

Relevant Pages

  • interface tcl to mingw dll which uses a msvc dll
    ... I'm not sure if this is a mingw or tcl problem. ... int ljackCmdHello (ClientData clientData, Tcl_Interp *interp, int ... Ljacktcl_Init(Tcl_Interp* interp) ... unsigned long *servicePackMajor, ...
  • Re: Need help with loading .SO file
    ... int GetText(ClientData clientData, ... int Proc_Init(Tcl_Interp *interp) ... Tcl_CreateCommand(interp, "gettext", GetText, ... reason to stay with Tcl 8.3 you should try to use a recent Tcl like Tcl ...
  • Re: C extension Clarification
    ... Is that we need to recompile the Tcl Source ... MyCmd (ClientData clientData, Tcl_Interp *interp, int objc, ...
  • Re: Obstacles for Tcl/Tk commercial application development ?
    ... Whenever there was a problem in a Tcl program, it was not related to the type system and would have occurred in any static language as well - but eventually much worse. ... "Simple typos" will come back to you as soon as you run your procedure the first time for testing. ... int fun { ...
  • Re: Best way to pass c++ pointer to tcl
    ... straightforward with simple types like int, long, etc., but I do not ... Creating and Using Tcl Handles in C Extensions: ... So my Ext_Init allocates a struct like: ... The performance with this method is superior to a hash table, ...