expected specifier-qualifier-list before ‘Button’

29/01/2010

What if you make a new struct, wich has a structmember that is a pointer to something that has the same type of the struct you are making?

For example:
typedef struct
{
char* text;
u8 posX;
u8 posY;
u8 width;
u8 height;
void* callback_function;
Button* array;
u8 itemsInArray;
} Button;

Now you will get the following error:  expected specifier-qualifier-list before ‘Button’
How to work around this?
Put struct in front of the structmember, and it will compile!

Working example:
ypedef struct
{
char* text;
u8 posX;
u8 posY;
u8 width;
u8 height;
void* callback_function;
struct Button* array;
u8 itemsInArray;
} Button;

Note:
The button-struct is not a very good struct itself, but it’s an example for this error.

Advertisements

Structs with function pointers with arguments

29/01/2010

Example: (complex, in principle only needed when you dive into C and are interested in pointers and void pointers)

http://cid-58814201e79976dd.skydrive.live.com/self.aspx/iepro/functionPointerExample.zip

This example has an example of making an array of objects (buttons) having as members function pointers.

These function pointers take arguments being  another member of the struct.

setting the pointers and its argument:

void setFunctionPointer_buttonView( void* myFunction, BUTTONVIEW* myButtonViewPtr, float myArgument ){
void (*foo)(float);
foo = myFunction;
((BUTTONVIEW*) myButtonViewPtr)->pushedButtonFunction = foo;
((BUTTONVIEW*) myButtonViewPtr)->argumentOfPushedButtonFunction = myArgument;
}

example of calling the function pointed at when clicked within the button:

void clickedWithinButton_buttonView( u8 clickPosX, u8 clickPosY, BUTTONVIEW* myButtonViewPtr ){

u8 positionX = myButtonViewPtr->posX;
u8 positionY = myButtonViewPtr->posY;
u8 _width = myButtonViewPtr->width;
u8 _height = myButtonViewPtr->height;

u8 left =   positionX;
u8 right =  positionX +  _width;
u8 up =   positionY;
u8 down =   positionY + _height;

if ( ((clickPosX > left) && (clickPosX < right)) && ((clickPosY > up) && (clickPosY < down)) ){
//playDrop() ;
void (*foo)(float);
foo = myButtonViewPtr->pushedButtonFunction;
float myANgle =  myButtonViewPtr->argumentOfPushedButtonFunction;
foo(myANgle);
}
}


Button struct pushed function pointer

14/01/2010

Imagine you have s struct which implements a button. One of the members of this struct is the function which is called when the button is pushed.

This is done with the function pointer, which is a void pointer (void*).

The principle can be found on interesting webpages like: http://www.cprogramming.com/tutorial/function-pointers.html.

There you see how it is done in a simple script:

void my_int_func(int x)
{
    printf( "%d\n", x );
}

int main()
{
    void (*foo)(int);
    foo = &my_int_func;

/////etcetera...

But how to do this in a struct? Let's do a test:
in a header file:

typedef struct{
 void *functionPtr;
 } FIEPOINTERTEST ;

 void sayHello();
 void  initTestStructAndSayGoodbye();
 void doFiePointer_buttonstruct( FIEPOINTERTEST* mytest);
 void fireFiePointer();

in a source file:

void  sayHello(){
 printf("   -------------------------\n" );
 printf("   ---greetings: zdravstbijtje-------------\n" );
 printf("   -------------------------\n\n" );
}

void  initTestStructAndSayGoodbye (){

 void (*foo)();
 foo = &sayHello;//init function pointer

 FIEPOINTERTEST * myTest = malloc( sizeof(FIEPOINTERTEST));
 myTest->functionPtr = (void*)foo;

//and init test struct
//and test the we need a function to fire the function pointer:

 fireFiePointer(myTest);//this is already testing
 fireFiePointer(myTest);
 fireFiePointer(myTest);
 fireFiePointer(myTest);
 printf ("doswidanja\n\n");//which is goodbye in russian

}

//so far so good, but how to implement the fireFiePointer method?

 void fireFiePointer_buttonstruct(FIEPOINTERTEST* mytest){
 void (*foo)();//make a function pointer
 foo = mytest->functionPtr; //give it the value of the member
 foo();//do the function call
 }

This does the job, but
what does not seem to work are things like:
mytest->functionPtr();

calling the object not a function......

So we can make the button - struct, with the image as a member and a function to be called


Making images, buttons and backgrounds

14/01/2010

In need of a nice glossy button? A background, a circular text on a marble texture?

Just found this site: http://www.grsites.com/

where you can easily generate all you need for a game…

lots of choices with examples readily available…

just made this button: (it even comes with a mouse_over highlight)

so we just need a nice struct for an image and a struct which extends this struct to a button struct 🙂

for example:

typedef struct{

char* imageTitle; // a string

u16* imageArrayBin; // pointer to the image_bin

u8 posX;

u8 posY;

u8 width;

u8 height;

} ImageView;

…..and…..

typedef struct{

ImageView* imageView;//pointer to the image of your button

u8 posX;

u8 posY;

FunctionPointer* buttonPushed;

} ButtonView;

oh yes, what was all that about pointers to functions????

It is just an easy way to implement what to do when the button is pushed…

look here: http://www.cprogramming.com/tutorial/function-pointers.html


Doubly-Linked List in C

07/01/2010

Since there wasn’t a good Linked List in C, I’ve created one. With a Linked List you can make dynamic array’s without throwing away memory. Normally you have to know the size of an array, but since this isn’t always known (for instance: if it depends on user input) it’s easier to use a Linked List.

It’s using the following concept (doubly linked) which is faster to add/edit/remove from a list:

How to create a list:

You will have to create a Header, this is the first element of the list, it’s the “start point” and “end point” of the list. You can create it like this:

struct CList *list;		// Will be the header
list = malloc(sizeof(struct CList));
CListCreate(list);

Now you can add values to the list (this can also be 32bits pointers). To add values, use the AddLast or AddFirst function:

CListAddLast(1, list);
CListAddLast(2, list);
CListAddLast(3, list);

You can iterate the list using the iterate function. It uses two variables to get it running. The first variable is used to store the current position of the iterator, and the second one is your return variable.

int iter = NULL;	// Empty iterator which stores the position of the iterator
struct CList *item;	// Empty pointer which will be filled using CListIterator
while(item = CListIterator(&iter, list))
{
	printf("val: %d ... \n", item->pointer);
}

To free the memory and destroy the list you can do this:

// Destroy list (and free it)
CListDestroy(list);
printf("size: %d ... \n", CListSize(list)); // Size should be 0
free(list);

Bugs?

Find them, fix them and store them in the comments 🙂

Download:

[ Download ]

Changelog:
– Removed “warnings” 07/01/2010 @ 14.30


C: pointerArrayExample

07/01/2010

Working with structs….:

Having a struct you need a pointer: (for passing by reference etc)

(this is a picture of a dutch pointer 🙂

SOMESTRUCT myExample;

SOMESTRUCT* myExamplePtr;

myExamplePtr = &myExample;

myExamplePtr->someElement = 78;

but this working with real variable and pointer becomes a bit cumbersome, why not only use the pointer?

SOMESTRUCT* myExamplePtr;

myExamplePtr->someElement = 78;

well then you app crashes! CRASH!

when you don’t bother to declare the real variable, you have to use malloc instead, making memory available:

SOMESTRUCT* myExamplePtr;
myExamplePtr= (SOMESTRUCT*) malloc( sizeof(SOMESTRUCT) );
if( myExamplePtr!= NULL ) {
  myExamplePtr->someElement = 254;
}
But dont forget to free this pointer:
free( myExamplePtr);

So these two ways of using pointers are equivalent, the normal declaration taking care of "garbage collection" the malloc way, is doing this yourself.

An example "DevCPP_mallocTest" using this malloc in a pointerarray is in the folder with the other examples:

http://cid-58814201e79976dd.skydrive.live.com/self.aspx/iepro/DevCPP%5E_mallocTest.zip

some basic  info about malloc:

http://www.elook.org/programming/c/malloc.html


C question time

28/12/2009

Having digested an introduction in C does not mean you are a seasoned C programmer!

hmmm is this the new nds?

Pointers are connected to real memory space. Pointers can be seen as globals. But pointers have the nasty behaviour of shutting of memory if you forget to free the memory space. Also the interplay between pointers and methods must be done carefully, not allocating memspace in a method and then returning a pointer to it. The free-ing process will get messy!

So possible trouble will be ahead! Working with malloc and free and pointers makes C programming really different from programming in JAVA or C#. You need another kind of thinking.

Here a the question: (hm it takes a bit of preparing, sorry)

Say we have a struct called LINE

This struct consists of two structs called POINT and three floats

struct POINT{

float x, y;

}

struct LINE

{

struct POINT* pointPTR1;

struct POINT*pointPTR2;

float cx, cy, cc;

}

What we need is two lines parallel to line someLine (someLinePTR) (bouncing a ball with width for instance needs this)

Normally (coming from JAVA or whatever) I would have this someLine declared and call a class method passing the variable to this someLine and returning an array consisting of two lines:

(returnArray *) giveMeTwoParallelLines( someLinePtr, distance){

struct LINE* myFirstParallelLinePTR = makeAParallelLine (someLinePtr, distance);//another method returns a pointer

struct LINE* mySecondParallelLinePTR = makeAParallelLine (someLinePtr, -distance);

//returning the results in an array

(struct LINE*) returnArray[] = {  myFirstParallelLinePTR , mySecondParallelLinePTR  };

return returnArray;

}

this method is called like this:

(struct LINE*) returnArray[] = giveMeTwoParallelLines( someLinePtr, 50);

elements from returnArray can be used further on.

What is wrong here? (Take the above as an indication how the task is done, details can be different)

This is the clue:

The compiler indicates (or the app crashes) memory leaks…

Having memory leaks means that memory is allocated but not freed. In Xcode (Objective C)you can run an app inside a memory leak analizer. Devcpp doesn’t offer this service!

The whole structure of making parallel lines must be solved differently. How?

Answer in some other blog!

This was a real stupidity I committed in XCODE objective C for the Iphone (a version of C that admits classes, but for the rest is like normal C.)

http://en.wikibooks.org/wiki/C_Programming/Memory_management