Skip to content

Instantly share code, notes, and snippets.

@okurka12
Created July 22, 2023 04:29
Show Gist options
  • Save okurka12/87d6bc2ca529aa9e68c9b195dae18772 to your computer and use it in GitHub Desktop.
Save okurka12/87d6bc2ca529aa9e68c9b195dae18772 to your computer and use it in GitHub Desktop.
Tabulkove makro v C
/******************************************************************************/
/* TABULKOVA MAKRA */
/******************************************************************************/
/**
* tak jak nam je ukazoval dr. Peringer 2. 5. 2023 na prednasce
* pry to objevil nekde v kodu GCC
*/
#undef ROW /* to be defined later */
/* Name, Surname, Age, City */
#define TABLE \
ROW("Vit", "Pavlik", 20, Brno) \
ROW("Prokop", "Buben", 30, Praha) \
ROW("Blanka", "Protrhla", 19, JindrichuvHradec)
/**
* V cem je figl? Krome retezcu nebo cisel muzu mit v tabulce ruzne veci:
* ukazatele, ukazatele na funkce a jine kulisarny.
*
* Takto muzu souvisejici polozky pridat jen na jednom miste a vubec needitovat
* kod na mistech kde se to pouziva.
*
* Vzdycky jen nadefinuju makro ROW pro vyber prislusneho sloupce.
*/
/******************************************************************************/
/* KOD: kdyz pridam do tabulky nejake polozky tak ho vubec neupravuji! */
/******************************************************************************/
#include <stdio.h>
/* enum of city identifiers */
#define ROW(name, surname, age, city) city,
enum cities {
TABLE
LENGTH /* sentinel value: number of items */
};
#undef ROW
int main(void) {
/* array of names */
#define ROW(name, surname, age, city) name,
const char *names[] = {TABLE};
#undef ROW
/* array of ages*/
#define ROW(name, surname, age, city) age,
int ages[] = {TABLE};
#undef ROW
for (int i = 0; i < LENGTH; i++) {
printf("%s is %d years old. (city = %d) \n", names[i], ages[i], i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment