vendredi 27 février 2009

workshop C II

In the first workshop we saw that C was about the same as JAVA, although C is not object orientated, in this workshop we see that there are a few differences...
To start with a real hurdle coming form JAVA we look at pointers.

pointers
http://home.netcom.com/~tjensen/ptr/pointers.htm

a pointer variable refers to an address in memory

int k; //makes an integer, not setting the value yet.

int *ptr;//makes an address variable, not setting this address either
//set aside however many bytes is required to store an address in memory

ptr = &k; //give the address variable the value of the address of the integer

*ptr = 7; //* is here the dereferencing operator
k = 7;//this last two lines have the same effect

pointer arithmetic is based on the size of the pointer's type
ptr++;//refers to the next address in memory, but shifts the space of an integer.

short m;
short *ptrShort = 6;
ptrShort++;//this would shift the space of a short

C will not permit the comparison of a pointer to type integer with a pointer to type character
that is why there are "void pointers"
void *vptr;// a generic pointer

sizeof(int)
is a function which returns the number of bytes used by the type provided. This is typically used in pointer arithmetic.

arrays

Arrays were introduced in workshop 1. But there is more than meets the eye, because arrays are big structures and for these structures C uses pointers.
So arrays and pointers are very related.

this is still like JAVA:
int my_array[] = {1,23,17,4,-5,100}; //integers stored continuously

But now we see the difference:
int *ptr;
ptr = &my_array[0]; /* point our pointer at the first integer in our array */

int i = 2;
my_array[i] refers to the same variable as *(ptr + i)) : 17

note: an arrayname is a pointer, although it seems a "normal: variable:
&arrayName[0] can be replaced with arrayName

then there is a curious C effect:
char myArray[5] = {'h', 'o', 'm', 'e', 'b', 'r', 'e', 'w', '\0'};
myArray[3] = *(myArray + 3) = 'm';
3[myArray] = 'm'; // because *(i + a ) = *( a + i) !


remember the multidimensional array? This is a pointer thing too, of course:
char multi[5][10];
*(*(multi + row) + col) is the same as multi[row][col]
because the elements are stored continuously in memory

to work with "raw" pointers, in pointerarithmetic and wanting to know exactly the place of a variable you use
sizeof(int).


structs

http://computer.howstuffworks.com/c31.htm
http://www.cprogramming.com/tutorial/lesson7.html

Although there are no objects, we can make or own complex variables, with members:
You can write your own data structure!

example:
typedef struct Triangle{

POINT p[3]; //member in this case another struct,
LINE l[3];//another struct as a member

int t[5];
int neighbours[25];
int outsidePoint[25];
int bouncers[25];

int neighbourNumber;
int outsideNumber;
int bouncerNumber;

}TRIANGLE; //this gives the struct a special name.

//so POINT en LINE zijn andere structs (convention = capitalized name)

struct Triangle myTriangle;//declaration or
TRIANGLE myTriangle;

members are defined with a point:
myTriangle.bouncerNumber = 7;


if you have a function you pass not the struct but a pointer to the struct:

*TRIANGLE myTrianglePointer;
myTrianglePointer = &myTriangle;

defining a member would become:
(*myTrianglePointer).bouncerNumber = 63;//but this can be written as:
myTrianglePointer-> bouncerNumber = 63;












strings/characters


a string is an array of characters terminated by '\0'

char my_string[40];

my_string[0] = 'T';
my_string[1] = 'e';
my_string[2] = 'd':
my_string[3] = '\0';

char my_string[40] = {'T', 'e', 'd', '\0',};

char my_string[40] = "Ted";

all these lines do the same thing, mind the different quotes.

standard string functions:
strcopy();
strlen();
strcat();
strchr();




functions

We already know of the pointer, and why we use pointers. In functions this is used:
Never send the struct itself, but always a pointer to the struct.
Of course this means that the struct itself will be changed in the function.

This is the difference:
passing by value
passing by reference -> pointers
since an arrayName is a pointer, fie(arrayName) passes the array by reference.



header files
You can write your whole project in the main, using functions on top (above) the main.
This can become rather a mess.
In C you can order your project in files seperated from the main.m

For instance, you can have a game.m file.
Then you have a header file for initialization of the functions used in the game.m file: game.h

the header files are collected in the folder called include, and the .m files in a folder called source.

You have to include the header files to let other files see the functions.

A problem can be that a header file is included several times, therefore in C you use a special header definition:
ifndef
#define
...
#endif


this prevents including the header twice, which would cause an error on function twice defined.

#ifndef __BACKGROUND_H__
#define __BACKGROUND_H__

#include

extern void initBackgrounds();
extern void fadeBackground(short screen, bool hide);
extern void setBitmapBackground(short screen, const void *bitmapData, int bitmapSize);
extern void setTiledBackground(short screen, const void *tileData, int tileSize, const void *mapData, int mapSize, const void *paletteData, int paletteSize);

#endif