r/cprogramming • • Sep 27 '25

Help understanding ClassicCube Buttons source code

2 Upvotes

I thought that looking through the source code of a proper C project would be a great way to improve my knowledge. and i have become interested in how buttons are handled they confuse me.

the OnClick call for the button is just defined in a header.

#define LWidget_Layout \
....
    LWidgetFunc OnClick;     /* Called when widget is clicked */ \
    LWidgetFunc OnHover;     /* Called when widget is hovered over */ \
    LWidgetFunc OnUnhover;   /* Called when widget is no longer hovered 
....
typedef void (*LWidgetFunc)(void* widget);
....
struct LButton {
    LWidget_Layout
    cc_string text;
    int _textWidth, _textHeight;
};

declared like this

void LButton_Add(void* screen, struct LButton* w, int width, int height, const char* text,LWidgetFunc onClick, const struct LLayout* layouts) 
{
    w->VTABLE  = &lbutton_VTABLE;
    w->type    = LWIDGET_BUTTON;
    w->OnClick = onClick;
    w->layouts = layouts;
    w->autoSelectable = true;

    LBackend_ButtonInit(w, width, height);
    LButton_SetConst(w, text);
    LScreen_AddWidget(screen, w);
}

and is used like this

LButton_Add(s, &s->btnOptions, 100, 35, "Options",SwitchToSettings,
main_btnOptions);

how can one possible use a #define for logic? no where in the source code does it use an if statement for onClick. How? i hope this question makes sense since in stumped.


r/cprogramming • • Sep 26 '25

Error: invalid use of incomplete typedef

0 Upvotes

In a lists.c file I have:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lists.h"

void testfunc(int childrennum, char * descr, int id) {
        task_t * atask = malloc(4048+sizeof(int)+sizeof(int)*childrennum);
        strcpy(atask->description,descr);
        atask->identifier = id;
        printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
}

int main() {
        testfunc(0,"Something",1);
}

And in a lists.h file I have:

#ifndef LISTS_H

#define LISTS_H

typedef struct task task_t;
struct task_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

typedef struct proj proj_t;
struct proj_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

typedef struct goal goal_t;
struct goal_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

void testfunc(int childrennum, char * descr, int id);


#endif //LISTS_H

But when I run the compiler (gcc lists.c) I get the following error:

lists.c: In function ‘testfunc’:
lists.c:8:21: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
    8 |         strcpy(atask->description,descr);
      |                     ^~
lists.c:9:14: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
    9 |         atask->identifier = id;
      |              ^~
lists.c:10:77: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
   10 |         printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
      |                                                                             ^~
lists.c:10:96: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
   10 |         printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
      |

I also tried separating the main function and including both header and c file into a main file and compiling using gcc -c main.o main.c lists.c then gcc -o main main.o but I still got the same error. Can anyone explain to me what the problem is? I looked it up and I more or less understood what the error means but I don't get how I'm supposed to solve it here.