Basic Tcl and C Inter Usage in IPlot

Combining TCL and C is very simple. Here are a few examples which should answer most questions.

Linking C and Tcl Variables

Many global variables are linked between C and TCL. This means that a change in a linked C variable through C code, will produce the same change in the equivalent TCL variable. Similarly, a change in a linked TCL variable by TCL code, will result in the same change to the C variable.

All linking for IPLot is done in the file tkAppInit.c Here is an example of each type of variable linkage.

The only difficulty may be in linking a string. What I did for the linked strings was to declare two variables like this. The first variable is an array which makes space in memory for the string. The second is a pointer to that space, and it is the pointer which gets linked. For some reason I had trouble linking to an array directly.

Calling C Functions from TCL Code

Any C function you wish to call from TCL code must be defined in the file tkAppInit.c

In Iplot these are all done identically. Here is an example. The function declaration must precede the act of defining it as a callable C function. I put my forward declarations directly in tkAppInit.c but they can be in a header file too.

Then you are ready to define the C function as something callable from TCL and give it a name with which TCL can refer to it. I made the names by which TCL code refers to my C functions the same name but with a prefix of C_.

To pass arguments to a C function from TCL simply call them in this manner

In the C code the arguments will be stored in the argument *argv[] and the number of arguments will stored in argc. argv[0] will always be the name of the function called. In this case argv[0] would be equal to "C_function"

Calling Tcl/Tk Functions from C Code

It is just as easy to call Tcl and Tk functions from C code. Here is an example.

int function(ClientData clientData, Tcl_Interp *interp,int argc, char *argv[]);

{
char cmd[64];
int code;

sprintf(cmd,"set x 1\n"); code = Tcl_Eval(interp,cmd);
printf("result %d - command %s\n",code,cmd);

}

The final print line is uneccessary, but will allow you to verify what the command was and whether or not it was correctly interpreted. If code equals zero, it was correctly interpreted.


IPlot Home Page Technical Doc's User's Guide
IPlot and its accompanying WWW pages were written by Gene McKenna
a member of Stanford University's famous TCAD Group