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: Unix & Linux Server
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)

💬 Dyskusja społeczności i komentarze

🌐 Dołącz do głównego czatu

Rozpocznij dyskusję w strumieniu transmisji Social Chat lub prześlij wyżej wkład ekspercki przez formularz uzupełnień.

Security Briefing Bezpłatnie · Wypisanie 1 kliknięciem

Intelligence Digest - bezpłatnie

Codziennie najważniejsze wiadomości bezpieczeństwa · można pobrać w każdej chwili

Community Rating & Social Proof
⭐ Ocena czytelników
–
Ø / 5.0
Brak recenzji czytelników
Czy ten artykuł był pomocny?
0
Dodaj swoją ocenę
Ocena szczegółowa (opcjonalnie)
Treść i jakość
Źródło i wiarygodność
Aktualność

Powiązany klaster opowieści i źródła (wektor AI)

6 Cluster
26 źródeł
Black Friday Oura Ring deals: The best of the deals so far, picked by a wearable-tech expert
Latest from TechRadar 17 minut temu
25 kolejnych źródeł
CNET6 lat temuOur Favorite Blenders of 2026 Are on Sale Ahead of Black Friday CNET21 minut temuBrighten Up Your Day by Saving 20% Off Flashlights With Olight’s Black Friday Sale CNET13 minut temuLive: Black Friday Deals Are Here. Shop Expert Picks Across Tech, Everyday Essentials and More at Up to 60% Off Golem.de27 minut temuAnzeige: Logitech-Tastatur MX Keys S am Black Friday zum Sensationspreis auf Amazon PC-WELT39 minut temuLuminar zum Black Friday: KI-Bildbearbeitung für nur 79,20 Euro Latest from TechRadar54 minuty temuThis is the pair of open earbuds to hunt down over Black Friday Latest from TechRadar1 godzinę temuBest AirPods Black Friday deals: all live savings on AirPods Pro 3 and more Computer Bild1 godzinę temuPhilips Sonicare DiamondClean 9000 zum Black Friday günstiger netzwelt.de2 godziny temuESR startet mit Black-Friday-Rabatten: Diese AirPods-Hüllen lohnen sich jetzt netzwelt.de2 godziny temuPlayStation 5 geschenkt: Diese Black-Friday-Aktion legt die Konsole gratis obendrauf netzwelt.de1 godzinę temu6 Dyson-Staubsauger im Vergleich: Dieser Sauger wird am Black Friday überraschend zum Preis/Leistungssieger netzwelt.de1 godzinę temuMonitore im Angebot zum Black Friday: Gutes Bild für Office, Gaming und mehr Golem.de57 minut temuAnzeige: Akkuschrauber mit 20 Bits zum Black Friday erstmals unter 50 Euro bei Amazon PC-WELT1 godzinę temuDie günstigsten Handytarife an Black Friday von 10 GB bis Unlimited: Hier lohnt sich ein Wechsel PC-WELT1 godzinę temuBlack Friday 2026 bei LiberNovo: Ergonomische Bürostühle mit bis zu 44 Prozent Rabatt Latest from TechRadar1 godzinę temuThe best Black Friday robot vacuum deals: Dreame, Shark, Dyson, and Roborock Golem.de1 godzinę temuAnzeige: Topdon-Starthilfe-Powerbank für Kfz zum Black-Friday-Bestpreis nur rund 85 Euro heise online News1 godzinę temuBlack Friday: Die besten Kopfhörer zum Tiefstpreis – Sonos Ace, Nothing & Shokz Latest from Windows Central1 godzinę temuDell's Black Friday deals drop major savings on Alienware OLED monitors CNET2 lata temuMake Every Night Game Night and Save $90 on the Nex Playground With This Black Friday Deal DEV Community1 godzinę temuThe app is part of the gadget: 14,000 App Store reviews of 28 device apps before Black Friday Latest from TechRadar2 godziny temuBlack Friday tech deals under $100 / £100: Fire TV Sticks, earbuds, coffee machines and more Caschys Blog3 godziny temuGoogle One: Black-Friday-Angebot mit bis zu 50 Prozent Rabatt netzwelt.de2 godziny temuDem Testsieger ebenbürtig: Zum Black Friday stürzt Xiaomis stärkster E-Scooter im Preis ab Macerkopf.de2 godziny temuFotos verbessern mit Aiarty: 52 % Rabatt + Gratisgeschenke zum Black Friday
Podobne wkłady
🔍 Verwandte News

Auch interessante Nachrichten Recursive type definitions in C

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

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Uwagi Komentarze są ładowane..
Rysunek do aktualizacji
Widgets & Topics
Loading widgets…
Nächster Beitrag