Page 1 of 1

PostPosted: Fri Feb 02, 2001 11:45 am
by psychic_treason
Okay, I know I have posted a lot of stuff on here recently, apologies to all the people that are bored with me now....

Anyway, can you create new types through STRUCTS or something similar in PocketC?

Any ideas would be cool.

PostPosted: Fri Feb 02, 2001 4:00 pm
by Justinespinosa
well, It's a bit hardier.
You can do that with structured pointers.

I don't know if you know how to use pointers, but with them , you can create stcuctured array of variable.
Darwin has explained it to me. That's like a tree view.
You can create a variable that have a lot of values of different types in it.

so this:

<pre id=code><font face=courier size=2 id=code>
pointer p;

p = malloc(10);

p[0] = malloc(5);

settype(p[0],5,'s'); // sets the type of the values in p[0]

</font id=code></pre id=code>

will create a struct like this in the variable p:

p = adress were p is stored
+-p[0] = adress were p[0] is stored
| +-p[0][0] = string value
| +-p[0][1] = string value
| +-p[0][2] = string value
| +-p[0][3] = string value
| +-p[0][4] = string value
+-p[1] = int value
+-p[2] = int value
+-p[3] = int value
+-p[4] = int value

to assign a value just do p[0][2] = "string";

to pass the variable to a function, just use the adress were the variable is stored.
(ex: Func_Do(pointer p) )

to pass the variable form a function, return the adress
(ex: <pre id=code><font face=courier size=2 id=code>
pointer p; // define a new pointer

p = malloc(...)

// assign parameters to pointer.

return p;
pointer Var:

Var = Func_New(parameters);
</font id=code></pre id=code>

Before re-allocing a pointer, you must free it with free(pointer p)

like this

p = malloc(10);

p[0] = malloc(5);

p[0][0] = malloc(2);

free(p[0][0]); // free last tree levels first

I hope this will help you. if you have questions or problems, just send me a mail or post on my forum on


OUTS Softwares

PostPosted: Sat Feb 03, 2001 1:41 pm
by psychic_treason
Thanks for the help Justin. Much appreciated.

This seems like a very convoluted way to build structures, but it seems that it will work reasonably well (slight concern with efficiency of larger structs). I'm still flipping my brain over from normal c/c++ into the whole pocketC thing.

Is there a place to request additions to the next version of the compiler/run time and possibly help writing it? It would be nice to see better support for things like structs, screen surfaces etc.


PostPosted: Tue Feb 06, 2001 8:51 am
by guy
PocketC (V3) supports structs, but not accessing fields in them through pointers.

struct mystruct {
int i ;
pointer p ;
char c ;
} ;

struct mystruct s ;

s.i = 1;
s.p = &something ;
s.c = 'a' ;

// All that works

pointer p ;

p = &s ;

p->i = 1;

// Doesn't work. PocketC can't resolve the offset.


PostPosted: Tue Feb 06, 2001 11:09 am
by darwin
Cool, I didn't know that!
Where was it mentioned that V3 support ALREADY structs?

PostPosted: Tue Feb 06, 2001 2:34 pm
by psychic_treason
Hey Justin and Guy, thanks for the help on this one. The stuff about the struct support was exactly what I was after.

Now I've got a new problem and I think it is just because I am being dim.

Here's the deal. I've set up a struct with all the bits in it that I want.

eg: struct node {
string key;
pointer lchild;
pointer rchild;

A binary tree type thing yeah? Okay, now obviously if I want to create a tree I have to call a function that creates each of the nodes and passes back something to the calling function. Here's a copy of what I'm doing:

pointer createGenNode(string key) {
// this function takes a string and creates a generic node, passing a pointer back to the
// caller with a reference to it.

struct nodeode newNode;

newNode.key = key;
newNode.lchild = NULL;
newNode.rchild = NULL;


return &newNode;


So I'm trying to pass back a reference to the node I just created. However I realised that I can't do this as I'm actually referencing a local variable which is destroyed after the function exits! Damn...

Next I tried to pass the structure back but that causes stack corruption. Any ideas?

I'd really like to implement a nice BTree for something I'm working on...


-- --

PostPosted: Wed Feb 07, 2001 8:06 am
by guy
I suggest that you look at the dynamic linked list/tree library rather than reinventing the wheel from scratch. It supports trees of lists, with various list and tree manipulation functions as well as node constructors and destructors and a tree walk with node entry and exit callbacks to functions that you supply.

The only thing it's really missing at the moment is a user callback for comparison so that entries can be sorted as they are inserted.

The latest version of the library in Unicode for the device compiler is at:

There is an updated version in ASCII with some additional features at:

If I have time today I'll add an insert sorted function to the ASCII version and update the library on the server.


PostPosted: Fri Feb 09, 2001 10:58 am
by darwin
If I cannot pass a struct to a function as an argument (am I wrong?), if I can't use it as a pointer - what are the structs good for?

PostPosted: Fri Feb 09, 2001 11:16 am
by guy
I agree.

Kevin said something to me about being able to pass structs around. Perhaps you can use them in function arguments.

Until I can use pointers to them so that I can malloc() them then they are not much use to me either. I also need a sizeof() function.

I don't know how Kevin will resolve the issue of addressing fields within structs through a pointer. Because pointers are untyped then finding the field name would be difficult:

struct first {
int i ;
int j ;
} ;

struct second {
int j ;
int i ;

pointer p ;

p = SomeFunctionReturningAPointer() ;

p->i = 2 ;

// How does PocketC know which "i" it is?
// Could be either field 1 or field 2


Edited by - guy on 02/09/2001 05:30:42

PostPosted: Fri Feb 09, 2001 12:08 pm
by psychic_treason
Indeed, this has been one of my biggest trials with pocketC (and no direct screen access grrrr).

I think there needs to be a concerted effort on this for the next version as it would be nice to have structs that work properly.

In the mean time, thanks for that code Guy, it worked a treat. It wasn't exactly right for what I wanted for the Binary Tree so I just took it and adapted it for my needs. I'll send you a copy if you want when I'm done so you have a specific BTree datastructure as well as a linked list one. - Very useful.

-- --

PostPosted: Tue Feb 13, 2001 8:26 am
by guy
Thank you, I would be very interested in a copy.

Would you like to add your new functions to the library so that they are available to everyone?

If they are significantly different then it may be unwise to include both lots since it will bulk out callers code. The desktop version of PocketC apparently only includes functions that are called in the final app file. I understand that the new version is only single pass so may not have this optimisation.


PostPosted: Tue Feb 13, 2001 8:39 am
by guy
White on lime green?!


PostPosted: Wed Feb 14, 2001 10:06 am
by psychic_treason

Some of it I modified from yours and some are new bits that are very specific to the Binary Tree ADT. I haven't quite finished it yet (I've actually *gone outside* for the last few days) but it shouldn't be much longer.

Yes Lime Green and white.... it was supposed to be fresh =) don't ask me, I just built the thing, it was my creative partner that came up with the colour scheme :P


-- --