Tcl_LinkVar(3)
okay bestie, welcome to Tcl_LinkVar(3) β a Tcl Library Procedure moment β¨ and we're linking Tcl variables to C variables, no cap π
πΈ NAME πΈ
Tcl_LinkArray, Tcl_LinkVar, Tcl_UnlinkVar, Tcl_UpdateLinkedVar β link Tcl variable to C variable. periodt.
β¨ SYNOPSIS β¨
#include <tcl.h>
int
Tcl_LinkVar(interp, varName, addr, type)
int 2
Tcl_LinkArray(interp, varName, addr, type, size) 2
Tcl_UnlinkVar(interp, varName)
Tcl_UpdateLinkedVar(interp, varName)
heads up, bestie: those little 2s are literally in the original synopsis, so I'm keeping them. character-for-character behavior π
π ARGUMENTS π
Tcl_Interp *interp (in)β the interpreter that containsvarName. Also used byTcl_LinkVarto return error messages. π«Άconst char *varName (in)β name of global variable. πΈvoid *addr (in)β address of C variable that is to be linked tovarName. InTcl_LinkArray, may beNULLto tell2Tcl to create the storage for the array2in the variable. π«int type (in)β type of C variable forTcl_LinkVaror type of array element forTcl_LinkArray. Must be one ofTCL_LINK_INT,TCL_LINK_UINT,TCL_LINK_CHAR,TCL_LINK_UCHAR,TCL_LINK_SHORT,TCL_LINK_USHORT,TCL_LINK_LONG,TCL_LINK_ULONG,TCL_LINK_WIDE_INT,TCL_LINK_WIDE_UINT,TCL_LINK_FLOAT,TCL_LINK_DOUBLE,TCL_LINK_BOOLEAN, or one of the extra ones listed below. InTcl_LinkVar, the additional linked typeTCL_LINK_STRINGmay be used. InTcl_LinkArray, the additional linked2typesTCL_LINK_CHARSand2TCL_LINK_BINARYmay be used. All the above for both functions may be optionally OR'ed withTCL_LINK_READ_ONLYto make the Tcl variable read-only. πTcl_Size size (in)β the number of elements in the C array.2Must be greater than zero. π
π DESCRIPTION π
okay so bestie, here's the whole vibe: Tcl_LinkVar uses variable traces to keep the Tcl variable named by varName in sync with the C variable sitting at the address given by addr. Like, a little long-distance relationship between your C code and your Tcl code, and honestly? it works. π«Ά
Whenever the Tcl variable gets read, the value of the C variable gets handed back. Whenever the Tcl variable gets written, the C variable gets updated to have that same value. Two-way street, no cap.
Tcl_LinkVar normally returns TCL_OK. But if something goes sideways while setting up the link (e.g. because varName is the name of an array) then TCL_ERROR is returned and the interpreter's result contains an error message. π
Tcl_LinkArray is similar, but for arrays of fixed size (given by the size argument). When it's asked to allocate the backing C array storage (via the addr argument being NULL), it writes the address that it allocated to the Tcl interpreter result. β¨
The type argument specifies the type of the C variable, or the type of the elements of the C array, and it must have one of the following values, optionally OR'ed with TCL_LINK_READ_ONLY:
TCL_LINK_INTβ the C variable, or each element of the C array, is of typeint. Any value written into the Tcl variable must have a proper integer form acceptable toTcl_GetIntFromObj; attempts to write non-integer values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too. πTCL_LINK_UINTβ the C variable, or each element of the C array, is of typeunsigned int. Any value written into the Tcl variable must have a proper unsigned integer form acceptable toTcl_GetWideIntFromObjand be in the platform's defined range for theunsigned inttype; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_CHARβ the C variable, or each element of the C array, is of typechar. Any value written into the Tcl variable must have a proper integer form acceptable toTcl_GetIntFromObjand be in the range of thechardatatype; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.
If you're using an array of these, consider using TCL_LINK_CHARS instead. π
TCL_LINK_CHARSβ the C array is of typechar *and is mapped into Tcl as a string. Any value written into the Tcl variable must have the same length as the underlying storage. Only supported withTcl_LinkArray.TCL_LINK_UCHARβ the C variable, or each element of the C array, is of typeunsigned char. Any value written into the Tcl variable must have a proper unsigned integer form acceptable toTcl_GetIntFromObjand be in the platform's defined range for theunsigned chartype; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.
If you're using an array of these, consider using TCL_LINK_BINARY instead. π§
TCL_LINK_BINARYβ the C array is of typeunsigned char *and is mapped into Tcl as a bytearray. Any value written into the Tcl variable must have the same length as the underlying storage. Only supported withTcl_LinkArray.TCL_LINK_SHORTβ the C variable, or each element of the C array, is of typeshort. Any value written into the Tcl variable must have a proper integer form acceptable toTcl_GetIntFromObjand be in the range of theshortdatatype; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_USHORTβ the C variable, or each element of the C array, is of typeunsigned short. Any value written into the Tcl variable must have a proper unsigned integer form acceptable toTcl_GetIntFromObjand be in the platform's defined range for theunsigned shorttype; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_LONGβ the C variable, or each element of the C array, is of typelong. Any value written into the Tcl variable must have a proper integer form acceptable toTcl_GetLongFromObj; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_ULONGβ the C variable, or each element of the C array, is of typeunsigned long. Any value written into the Tcl variable must have a proper unsigned integer form acceptable toTcl_GetWideIntFromObjand be in the platform's defined range for theunsigned longtype; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_DOUBLEβ the C variable, or each element of the C array, is of typedouble. Any value written into the Tcl variable must have a proper real form acceptable toTcl_GetDoubleFromObj; attempts to write non-real values into varName will be rejected with Tcl errors. Incomplete integer or real representations (like the empty string,'.','+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too. πTCL_LINK_FLOATβ the C variable, or each element of the C array, is of typefloat. Any value written into the Tcl variable must have a proper real form acceptable toTcl_GetDoubleFromObjand must be within the range acceptable for afloat; attempts to write non-real values (or values outside the range) into varName will be rejected with Tcl errors. Incomplete integer or real representations (like the empty string,'.','+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_WIDE_INTβ the C variable, or each element of the C array, is of typeTcl_WideInt(which is an integer type at least 64-bits wide on all platforms that can support it). Any value written into the Tcl variable must have a proper integer form acceptable toTcl_GetWideIntFromObj; attempts to write non-integer values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too. πTCL_LINK_WIDE_UINTβ the C variable, or each element of the C array, is of typeTcl_WideUInt(which is an unsigned integer type at least 64-bits wide on all platforms that can support it). Any value written into the Tcl variable must have a proper unsigned wideinteger form acceptable toTcl_GetWideUIntFromObj; attempts to write non-integer values into varName will be rejected with Tcl errors. Incomplete integer representations (like the empty string,'+','-'or the hex/octal/decimal/binary prefix) are accepted as if they are valid too.TCL_LINK_BOOLEANβ the C variable, or each element of the C array, is of typeint. If its value is zero then it will read from Tcl as"0"; otherwise it will read from Tcl as"1". Whenever varName is modified, the C variable will be set to a0or1value. Any value written into the Tcl variable must have a proper boolean form acceptable toTcl_GetBooleanFromObj; attempts to write non-boolean values into varName will be rejected with Tcl errors. β¨TCL_LINK_STRINGβ the C variable is of typechar *. If its value is notNULLthen it must be a pointer to a string allocated withTcl_Alloc. Whenever the Tcl variable is modified the current C string will be freed and new memory will be allocated to hold a copy of the variable's new value. If the C variable contains aNULLpointer then the Tcl variable will read as"NULL". This is only supported byTcl_LinkVar. πΈ
okay so if the TCL_LINK_READ_ONLY flag is present in type, then the variable will be read-only from Tcl, so its value can only be changed by modifying the C variable. Attempts to write the variable from Tcl will be rejected with errors. π«
Tcl_UnlinkVar removes the link previously set up for the variable given by varName. If there does not exist a link for varName then the procedure has no effect. (No drama, no error, just nothing. iconic tbh.)
Tcl_UpdateLinkedVar may be invoked after the C variable has changed to force the Tcl variable to be updated immediately. In many cases this procedure is not needed, since any attempt to read the Tcl variable will return the latest value of the C variable. However, if a trace has been set on the Tcl variable (such as a Tk widget that wishes to display the value of the variable), the trace will not trigger when the C variable has changed. Tcl_UpdateLinkedVar ensures that any traces on the Tcl variable are invoked. π₯
Note that, as with any call to a Tcl interpreter,Tcl_UpdateLinkedVarmust be called from the same thread that created the interpreter. The safest mechanism is to ensure that the C variable is only ever updated from the same thread that created the interpreter (possibly in response to an event posted withTcl_ThreadQueueEvent), but when it is necessary to update the variable in a separate thread, it is advised thatTcl_AsyncMarkbe used to indicate to the thread hosting the interpreter that it is ready to runTcl_UpdateLinkedVar. π§΅β¨
lowkey the most important part of this whole page, bestie. threads do not play. π
π KEYWORDS π β¨
okay bestie, this is the lil tag cloud at the bottom β the words you'd smash into man -k or your search bar when you're like "where is that page about linking a variable to a C variable again??" π These are the searchable vibes this page is filed under:
booleanβ for when you want a variable that's justtrue/falseenergy πintegerβ whole numbers only, no decimals allowed, periodt πlinkβ the whole point of this page honestly, the link between your Tcl variable and your C variable πread-onlyβ for the ones you can look at but cannot touch π«realβ floating point girlies, decimals welcome πstringβ just vibes, just text, anything goes πtraceβ the behind-the-scenes magic that makes the link update β¨variableβ the main character of this entire manpage, no cap π
and that's a wrap on Tcl_LinkVar(3) π«Ά this lil icon of a page comes to you from Tcl 7.5 β yes, she's been around a while and she's still slaying π
no date stamp on this footer, just the version doing its thing, iconic behaviour fr fr πΈ
go link some variables bestie, you got this πβ¨π¦
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms