Zum Hauptinhalt springen
Intelligence View
⚡ tsecurity.de Intelligence

Recursive type definitions in C

Hi,It's been a while -- I've been mostly busy with university. Maybe I won't try to follow four classes next year. In other news, we didn't make it (with…

Beitrag
0
Seite
0
↗ Quelle (vegardno.net)
Cloud & Tech-Stack · 2 Min. Lesezeit
Quelle: vegardno.net
Rubrik: Server Unix e Linux
Hi,

It's been a while -- I've been mostly busy with university. Maybe I won't try to follow four classes next year. In other news, we didn't make it (with kmemcheck) for 2.6.28 either. Oh well. We did at least make an impression by discovering two more bugs in 2.6.28-rc.

Now for the topic of this post: Recursive type definitions in C. More specifically, the types I want to write about are not structs, but function pointers. We may use typedefs to define types which refer to function pointers:

typedef void (*funcpointer_t)();

Not strictly necessary to make it a new named type, but it helps readability. This is all well and good; we can declare variables that hold function pointers, declare functions that take function pointers as parameters, or even declare functions that return function pointers (to functions with this specific signature):

funcpointer_t x;

void do_a(funcpointer_t f);

funcpointer_t do_b(void);

Now, what I really wanted to do was to create a parser in C where a variable would hold the next function to call, and where the function itself returned a (function) pointer to the next function to call (i.e. the next state of the parser). It would look something like this:

funcpointer_t init_state(const char *token) {
/* ... */
return &some_other_state;
}

funcpointer_t some_other_state(const char *token) {
/* ... */
return &some_other_state;
}

void parse() {
funcpointer_t state = &init_state;

while (token = read_token())
state = state(token);
}

Okay, so this looks quite good. But how do we define funcpointer_t? Defining structs that have pointers of the same type is quite easy, since the compiler already knows about the type as soon as you start defining it. The most obvious example is the linked list:

struct list_node {
struct list_node *next;
/* ... */
};

Another possibility includes forward declarations, where we simply declare that a type exists, without defining it:

struct a;
struct b {
struct a *x;
};

The same goes for functions. They can refer to themselves, since we must provide their name at the start of their definition, or declare them beforehand. But how can we do this with function pointers?


Vegard
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!
Informations-Bewertung:
✏️ Information ergänzen
🛡️ Versioniert via CMS-Revisions-Engine (Freigabe-Queue für Gäste)

💬 Discussione della community e commenti

🌐 Entra nella chat principale

Avvia la discussione nel flusso broadcast della Social Chat oppure invia qui sopra un contributo esperto tramite il modulo di integrazione.

Briefing di sicurezza Gratuito · disdetta in 1 clic

Intelligence Digest — gratuito

Ogni giorno le notizie di sicurezza più importanti · annullabile in qualsiasi momento

Valutazione della community & prova sociale
⭐ Giudizio dei lettori
–
Ø / 5.0
Ancora nessuna valutazione dei lettori
Questo articolo ti è stato utile?
0
Lascia ora la tua valutazione
Valutazione dettagliata (facoltativa)
Contenuto & qualità
Fonte & credibilità
Attualità

Cluster di storie correlate & fonti (IA vettoriale)

6 Cluster
24 fonti
Black Friday tech deals under $100 / £100: Fire TV Sticks, earbuds, coffee machines and more
Latest from TechRadar 1 h fa
23 altre fonti
Caschys Blog2 h faGoogle One: Black-Friday-Angebot mit bis zu 50 Prozent Rabatt netzwelt.de1 h faDem Testsieger ebenbürtig: Zum Black Friday stürzt Xiaomis stärkster E-Scooter im Preis ab Macerkopf.de1 h faFotos verbessern mit Aiarty: 52 % Rabatt + Gratisgeschenke zum Black Friday CNET11 giorni faGrab Our Top Gaming Chair Pick for up to $200 Off in Secretlab’s Black Friday Sale Latest from TechRadar1 h faWalmart's Black Friday deals are live — shop our top picks now Latest from TechRadar1 h faLive: Apple Black Friday deals — save on AirPods Pro 3, iPad 11-in, Apple Watch 12, AirTags & more WIRED1 h faThe Best Early Black Friday MacBook Deals (2026) Latest from Windows Central1 h faEssential Xbox Ally travel accessories drop their prices before Black Friday IT Security News1 h faBeyond Black Friday: The Cyber Threats Retailers Need to Prepare for in 2026 Latest from TechRadar1 h faBest Black Friday streaming deals from 99 cents: Apple TV, HBO Max, Paramount+, Peacock, and more CNET1 h faThis Bissell Vacuum Tackles Tough Pet Hair and It’s Just $170 During the Black Friday Sales WIRED1 h faGPS Dog Collar for Less: Our Top Tested Picks From Fi’s Black Friday Sale (2026) Latest from Windows Central1 h faDell's excellent XPS laptops are up to 33% off in this early Black Friday sale WinFuture News2 h faBlack Friday bei VZMore: Bis 600 Euro Rabatt auf Power-Mini-PCs Latest from TechRadar2 h faThe best Black Friday turntable deals, as picked by an audio editor Latest from TechRadar1 h fa40+ best Black Friday deals in the UK — Switch 2, Shark vacuums, iPhone 18, Lego, PS5 bundles, Samsung, LG OLED TVs, and more CNET12 giorni faUpgrade Your Home Security for Less. Score Up to 65% Off With These Black Friday Ring Deals Giga.de2 h faVor Black Friday zu viel bezahlt? Mit einem Trick könnt ihr doch noch sparen - Futurezone1 h faStreaming-Sticks zur Black Week: Mit diesen Deals sparst du bis zu 46 % t3n.de2 h faBlack Friday: Diese Samsung-Angebote bei Amazon und Media Markt lohnen sich netzwelt.de4 h faBlack-Friday-Kracher: Huawei-Smartwatch mit EKG für unter 150 Euro Golem.de2 h faAnzeige: Sony WH-1000XM6 Referenz-Kopfhörer am Black Friday zum Amazon-Bestpreis Macerkopf.de3 h faAmazon Black Friday: Apple Pencil, WISO Steuer 2027, Microsoft 365, iPhone 18 Cases und mehr
Articoli simili
🔍 Verwandte News

Auch interessante Nachrichten Recursive type definitions in C

Thematisch verwandte Begriffe: Recursive, type, definitions · 6 Treffer

Laden...

Caricamento dei video ...

Laden...

Caricamento degli articoli ...

Laden...

Caricamento dei video ...

Laden...

Caricamento degli articoli ...

Laden...

Caricamento dei video ...

Laden...

Caricamento degli articoli ...

Laden...

Caricamento dei video ...

💬 Caricamento dei commenti…
Trascina per aggiornare
Widgets & Topics
Loading widgets…
Nächster Beitrag