[squeak-dev] Local variable in plugin

David T. Lewis lewis at mail.msen.com
Tue Nov 4 00:54:24 UTC 2008


On Sun, Nov 02, 2008 at 07:34:18PM -0800, Ang Beepeng wrote:
> 
> How to initialize a local variable in plugin?
> 
> Say I want to have smething like this in c.
> 
> int myfunction (){
>    int* temp;
> 
>    for(int i=0; i< 4; i++){
>    temp[i] = i;
>    }
> }

Well, you would not want to do that in C, because your temp
variable is not pointing to any allocated memory. In this case
you would want to declare it is "int temp[4]".

To write it in Smalltalk, you could write this:

myFunction
    | temp i |
    self var: #temp declareC: 'int temp[4]'.
    i := 0.
    [i < 4] whileTrue:
            [temp at: i put: i.
            i := i + 1]

Which will be translated to C:

static sqInt myFunction(void) {
    sqInt i;
    int temp[4];
    i = 0;
    while (i < 4) {
        temp[i] = i;
        i += 1;
    }
}





More information about the Squeak-dev mailing list