PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Query about "list" behaviour

I have attached a zip archive of a code example where I have created a list where each element is a structure. The structure is meant to be three simple strings.

As per my code I am having trouble reading back the strings with a "showList" proc.

Can anyone tell me what i am doing wrong?

I would also be interested in a comment as to whether the mechanism by which I return the string from my GetArgStr() proc is valid ... does the mechanism return a persistent result that does not go out of scope. I must admit I am struggling at times to understand "scope" in relation to returning values from procedures.

thanks in anticipation
Attachments:
list.zip (3 KB)
Don't forget that strings in structures elements the size of TINT or TDOUBLE, it only stores the string's pointer. Therefore you have to use the ^ operator to cast the pointer to the string.

ex:
Code:
struct(s$, "str1", "str2", "str3"); // Default size is TINT
s.str1$ = "String1";
s.str2$ = "String2";
s.str3$ = "String3";
ShowMessage(s.str1$); // Returns pointer.
ShowMessage(^s.str1$); // Display correct string.

This also apply for arrays:

ex:
Code:
Dim(a$, 100);
a$[10] = "String10";
ShowMessage(a$[10]); // Pointer
ShowMessage(^a$[10]); // String

Regards,
Kornalius
mervjoyce · 27 Jul 2004, 22:40 UTC — Thanks
Kornalius,

Thanks for the explanation.

I had already found by trial and error that the pointers needed to be de-referenced. I was building a structured list an element at a time and testing after I added each new element. I thought I understood it, but then I got a situation where the de-referencing caused the program to crash.

With some confidence from your last response that de-referencing is the correct approach I investigated further. The problem was solved by forcing str() typing during the display :

Code:

showmessage("x,y "+str(^MyList.str1$))+","+str(^MyList.str2$));


thanks for your reply
Strange, the ^ operator should convert the pointer to string automatically. I will do some testing tomorrow to see if it is not broken or something.

Regards,
Kornalius
PointOfLight · 28 Jul 2004, 14:26 UTC
I had been playing with mervjoyce's program before your first response, Kornalius, and I ran into the same problem with it crashing. That's why I never posted anything on this thread. Once I saw your response, though, I tried it again and what I discovered was that you didn't need to put str around it, but you do need to put parentheses around the item - for instance, (^MyList.str1$). Might this possibly be because it's part of a structure?
That would make sense then, because the ^ is used in an expression.

Ex:

dim(a$, 10);
a$[0] = "ABC";
a$[1] = "DEF";

ShowMessage(^a$[0]+","+^a$[1]);

The + "," is why it is crashing. Because you can do:

ShowMessage(^a$[0]+2); // display "C".

I guess then just an str() is safer:

ShowMessage(str(a$[0])+","+str(a$[1]));


Regards,
Kornalius