Quick actions

cmd+k|ctrl+k

Navigation

Languages

Overloaded [ ][ ]

Snippet info

Language

Cpp

Visibility

public

Author

evine

Created

2016-05-20T17:55:06Z

Updated

2016-05-21T04:45:01Z

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


struct Screen{
    char *thing;
    int width;
    int height;
    
    struct TempStruct{
        Screen *screen;
        int x;
        
        char& operator[](int y)
        {
            return screen->thing[y * screen->width + x];
        }
    };
    
    TempStruct operator[](int x)
    {
        return TempStruct{this,x};
    }
};

Screen screen_init(int width_in, int height_in)
{
    Screen result;
    result.thing = (char *)calloc(width_in * height_in + 1,1);
    assert(result.thing);
    result.width = width_in;
    result.height = height_in;
    return result;
}

int main() {
    Screen test = screen_init(5,2);
    
    
    for(int i = 0; i < test.width; ++i)
    {
        test[i][0] = "eple\n"[i];
        test[i][1] = "pera\n"[i];
    }
    
    test[2][1] = 'T';
    test[0][1] = test[2][0];
    
    printf("%s",test.thing);
    return 0;
}
INFO