Calculator Community > Calculator C

crash after calling function twice

(1/1)

klebue:
Hi there!
I'm trying to get into c programming for nspire (actually I'm trying to get into c programming at all ;) )

I can have a very simple function, say to draw a dot of 2*2 pixels.
The function works just fine and draws my dot, but when the function is called for a second time the nspire crashes.
Here is the code I use:

--- Code: ---int main(void) {
clearScreen();

void drawdot(int posx, int posy, int colour) {
setPixel(posx, posy, colour);
setPixel(posx+1, posy, colour);
setPixel(posx, posy+1, colour);
setPixel(posx+1, posy+1, colour);
}

drawdot(10,10,0); //works fine
drawdot(10,15,0); //crashes when I add this line

while (!isKeyPressed(KEY_NSPIRE_ESC)) {}
return 0;
}

--- End code ---

There is also another problem: When I press ESC the calculator will stop responding instead of redrawing the normal screen.
What am I missing to make him do so? I can't figure out what makes it work in other programs.

Thanks in advance!

bwang:
Don't define your functions inside main().
In fact, place main() in its own file and all your functions in other header files. I've had trouble with functions defined in the same file as main() crashing.

klebue:
Thanks for the advice, the function thing is working now.
However, I still couldn't figure out how to quit a program and make the calculator continue working the old-fashioned way.

/edit:
problem solved, the last lines should be

--- Code: ---while (!isKeyPressed(KEY_NSPIRE_ESC)) {
rand();
}
return 0;
--- End code ---
The nspire doesn't like doing nothing ;)

bwang:
while (!isKeyPressed(KEY_NSPIRE_ESC)); also works.

Navigation

[0] Message Index

Go to full version