lundi 6 octobre 2008

c programming

For the nds in DevkitPro we can program either in C or C++.

Here are some references for things which are a bit different in C:
http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays

Also you have to be careful with strings, which actually don't exist in C.
char c;
char string[100]; //a string
char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};
char month1[]=”January”;
strcpy(mystring, "hello");//to initialize a string
strcpy(string1,string2);//copying
strcat(mystring,"hello");//got paste strings together

Character string terminated by a null character ‘\0’.

see for instance:
http://www.exforsys.com/tutorials/c-language/handling-of-character-strings-in-c.html

converting an int to a string, very basic:
u16 strokes = 12;
u8 s10 = (u8)(strokes/10);
u8 s1 = (u8)(strokes - s10*10);

char store[] = { (char)(s10 + 48) , (char)(s1+48) , '\0'};

mind the difference between " and '

Arrays:
See link on top of page.
Char string[] is already an array.
There is no evident way to get the size of an array, be careful, my trick was to put a zero at the end, and test for the zero.

Headers and Source files
You can put all the methonds or functions in the header, but accumulating headers like this has put me into trouble, so separate the method and the headers.

Don't forget to put at the top of the header file:
#ifndef __headername_H_
#define __headername_H_
and close with
#endif

otherwise you get the compiler complaining about multiple definitions of methods