Need help rewriting the C macro as a function

I need help rewriting the next line in a safer way and rewriting it as a function, however, the fact that this code is defined inside the function makes it difficult for me to think of a smart way to do this because it seems to be related to declaring a few arguments.

#define CHECK(id) if(table->cells[id]) isgood[table->cells[id]-1] = 0;

where table- struct, and isgood- int.

+3
source share
6 answers

Direct translation (if table-> cells [id] is int):

void check(int id, int*isgood) { if (id) isgood[id-1] = 0; }

Call using:

check(table->cells[id], isgood);

/. . , .. table- > cells [id] == 0, isgood [-1], .

+5

,

?

+4

, table id ?

void foo(TableType & t, int id)
{
    if (t.cells[id]) 
        isgood[t.cells[id]-1] = 0;
}

p.s.

. .

p.p.s.

, . ?

+2

++, , - , :

class Table {
    //...
    public bool check(int id) {
        if (this->cells[id]) {
            this->isGood[id] = 0;
            // the line you have, isgood[table->cells[id]-1] = 0 looks buggy:
            // you treat table->cells[id] as a true/false value one line ago;
            // probably not a valid array index? I'm taking a stab at what to do.
        }
    }
}
+2

, .

First, make sure the name makes sense. what are you checking And is there a side effect?

void update_valid_cells(Table& table, int id, BoolArray& validArray)
{
     if(table.cells[id]==NULL) return;
     validArray[id]-1=false;
}
+1
source

I think C99 can qualify functions as built-in, so you get no-function-call acceleration without using macros. In addition, most C compilers support extensions such as __inline for this purpose.

+1
source

Source: https://habr.com/ru/post/1705485/


All Articles