Binding an array

Hi to all, I'm new to this forum and to this programming language...
i've got a problem with my thesis, I'm working with NS-2 and i need to bind an array of integers between TCL and C++... i think i have a problem on C++ side, i don't know the correct syntax...

I did like this, into the .h file i've declared the array, called seq_ like this:

int seq_[];

as a public array

into the .cc file i'm trying to bind it:

bind("seq_", seq_);

the array's name is the same in both TCL and C++ sides.

what's wrong with it?

thank you very much

It doesn't work that way.

Unless you are using a library I am unaware of, there is no "bind" function in the Tcl library.

The thing most analogous to a C++ array in Tcl is a list. To manipulate lists, you need the Tcl_ListObj commands:
http://www.tcl.tk/man/tcl8.5/TclLib/ListObj.htm

Notice that each element of the list is a Tcl_Obj. That means that the integers in your array must also be converted to Tcl_Objs before you can append them. Do that with the Tcl_IntObj functions:
http://www.tcl.tk/man/tcl8.5/TclLib/IntObj.htm


Now all you need are two functions to reconcile your C++ integer array with the Tcl integer object list; one to copy the array to the list, and the other to copy the list to the array.

Hope this helps.
yes very helpful, thanks,

i thought i could manage arrays in TCL, thus i've initialized my array in my tcl script following this:

http://www.tcl.tk/man/tcl8.4/TclCmd/array.htm

by setting, as example:

array set seq_ {}

i thought it was correct, but it is not.

now i try with lists as you suggested me, thanks.
Well, that is correct -- from the Tcl side. The interpreter makes handling things like that easy.

From the C++ side there are a few more hoops to jump through, but the Tcl library makes it very easy as well.

Good luck!
Topic archived. No new replies allowed.