From: Kevin Walzer on
Hi all,

I'm trying to extract the string values of a Tcl list in C code and am
having some difficulty. Tcl_GetString doesn't seem to work here:

Tcl_Obj *data = NULL;
Tcl_Obj *indexptr = NULL;

for (i = 0; i < 10; i++) {

status = Tcl_ListObjIndex(interp, mylist, i, &indexptr);
data = Tcl_GetString(&indexptr);
printf("Status=%s (%s)\n", data, TCL_OK);fflush(0);

//convert listobjindex to string to do other stuff with here

}

I thought Tcl_ListObjIndex returned a string representation stored in
the "indexptr" pointer, but apparently it does not--unless I'm doing
something wrong, which is quite possible. Here, the printf statement
returns some gibberish that I can't decipher--not the string value of
the index, i.e:

Status=?Ս ((null))


How can I obtain the string value at Tcl_ListObjIndex?

--Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
From: brett on
On Apr 6, 8:36 pm, Kevin Walzer <k...(a)codebykevin.com> wrote:
> Hi all,
>
> I'm trying to extract the string values of a Tcl list in C code and am
> having some difficulty. Tcl_GetString doesn't seem to work here:
>
>    Tcl_Obj *data      = NULL;

You don't want data to be a Tcl object, do you? Not sure if that's the
whole problem, but you'll want that a char.

HTH,
--brett

From: Donal K. Fellows on
On 07/04/2010 04:36, Kevin Walzer wrote:
> I'm trying to extract the string values of a Tcl list in C code and am
> having some difficulty. Tcl_GetString doesn't seem to work here:
>
> Tcl_Obj *data = NULL;
> Tcl_Obj *indexptr = NULL;
>
> for (i = 0; i < 10; i++) {
>
> status = Tcl_ListObjIndex(interp, mylist, i, &indexptr);
> data = Tcl_GetString(&indexptr);
> printf("Status=%s (%s)\n", data, TCL_OK);fflush(0);
> }

Tcl_ListObjIndex returns the value at a particular index of a list (via
the last argument) or NULL if you're off either end of the list.
Tcl_GetString returns the UTF8-ish string representation of any object,
but can't cope with NULL at all. Your code above shouldn't compile due
to a type mixup in the data argument, and you should also check for NULL.

Donal.