Kihagyás

8. előadás

Artifacts

String II

Debaggálás II

Intédzser 0

Legászi I -- first occurence documented

STRUKT - I

DIZÁJNÁT - I (designate)

Függvénymutatók

double (*fp)(double); //fp pointer deklarálása, amely egy függvényre mutat
                      //Ennek a függvényke a return értéke double
                      //a paramétere szintén double

fp = sin;             // fp definiálása a `sin` függvényre


if (NULL == fp) { // apparently it can be NULL sometimes? :thinking:, mondjuk ha nem lett beimportálva?? I suppose
    return; // vagy a user egy fucking troll és NULL-t adott c:
}

double result;
result = (*fp)(0.5); //Ez a két sor ekvivalens
result = fp(0.5);

double (*gp)();
gp = fp;
gp(0.5); // Szintén meghívja a `sin`-t, de a paramétereket nem ellenőrzi

Pointer-aritmetika nem alkalmazható függvény pointerekre.

typedef - Type aliasing

Primitív típusok

typedef int index_t; // index_t az int szinonímája

Függvények

typedef double (*trigo_func)(double); // trigo_func a double(double) mutató szinonímája

Structok

typedef struct {
    char name[];
    unsigned int age;
} Person;

Person a; // mostantól nem kötelező a struct keyword használata c:

Függvénymutatók példa

typedef double (*trigo_func)(double);

// Function that returns the inverse function for the passed trigo function
trigo_func inverse(trigo_func fun){
    static trigo_func from[] = { sin,  cos,  tan};
    static trigo_func   to[] = {asin, acos, atan};

    for(int i = 0; i < sizeof(from)/sizeof(*from); ++i){
        if (fun == from[i]) return to[i];
    }

    return fun;

}

Összetett adatszerkezetek

enum - Felsorolási típus

enum Color {WHITE, BLACK, RED, YELLOW, GREEN};

enum Color read_light();

void f() {
    enum Color traffic_light = read_light();

    switch (traffic_light) {
        case RED:
            puts("Your mom");
            break;
        case WHITE:
            puts("What kind of weird ass traffic light is this");
            break;
        case BLACK:
            puts("It brokey");
            break;
        default:
            puts("RGB party time");
            break;

    }
}

Saját értékek megadása

enum Color {A, B=2, C, D, E=1, F=A+B}
// Results in: {0, 2, 3, 4, 1, 2}

Struct

struct Date {
    int year;
    int month;
    int day;
}

void f(void){
    struct Date exam = {2018, 12, 17};
    struct Date *ep = &exam; //pointer az `exam`-re

    ++exam.day;      //
    ++(*ep).day;     // Ez a 3 sor ekvivalens
    ++ep->day += 2;  //
}
struct square
{
    int centerX;
    int centerY;
    int side;
};

struct square move( struct square s, int dX, int dY)
{
  s.centerX += dX;
  s.centerY += dy;
  return s;
}

void f(void)
{
  struct square mySquare = { 0, 0, 10 }; /* inicializáció */

  mySquare.centerX += 20;      /* mozgassuk el mySquare-t */ 
  mySquare.centerY += 30;      /* a (+20,+30) vektorral   */ 

  mySquare = move(mySquare, 20, 30);  /* uaz. függvénnyel */
}

Únió

Cursed child