[Tutor] subscript problem

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue Jun 3 13:03:03 2003


> I am having a problem with handling some pointers. My problem is like
> this:
>
> typedef struct ab
> {
>       short n_p;
>       Vec* po;
>        short* con;
> } AB;
>
> typedef struct vec
> {
>       int x;
>       int y;
> } Vec;


Hi Jimmy,


Ok, all of the code you've shown at the moment is in C; I'll assume, for
the moment, that you're trying to figure out how to do a translation of it
into Python.



> Now the function:
>
> XYZ(AB* out)
> {
>         last = out->con[n];            // last is of type int.
>         lim = out ->po + last;        // lim is of type Vec.
>
>         v_start = out -> po[first];   // v_start is of type Vec.
> }
>
>
> The problem is that when i use po[first] it gives an error message
> saying that
>
> AttributeError: Vec instance has no attribute '__getitem__'


This is difficult to decipher, as you haven't shown us any Python code
that implements your Vec vector class.  *grin*


But you probably have a type problem: your structure 'ab' should contain a
list of vectors, referred to by 'po'.  In C, a pointer to a structure can
refer to a contiguous sequence of elements.

The attribute error, though hints that the 'po' attribute isn't a list.
But without seeing any code, we are really grasping at straws.



> Can someone help me in sorting out the code for this in python.

You have to show us your Python code first.  *grin*


Good luck to you.