aod
aod

Reputation: 77

Issue in converting a macro to a function [C]

I have the following function, which I am trying to convert to a macro.

void hex_display(void const *v_ptr, int num)
{
    uchar const *ptr = v_ptr;
    int i;
    for (i=0; i<num; i++)
    {
        if(i != 0)
        {
            if(((i%4) == 0) && ((i%16) != 0))
            {
                fprintf(fp, "\t");
            }
            if((i%16) == 0)
                fprintf(fp, "\n");
            fprintf(fp, "%02x", ptr[i]);
        }
        else
            fprintf(fp, "%02x", ptr[i]);
    }
    fprintf(fp, "\n");
}

When I try to convert it to a macro like below, compilation errors like macro parameters must be comma-separated, "*" may not appear in macro parameter list are coming right on the first line. Not sure how to tackle this.

#define hex_display(void const *v_ptr, int num)\
{\
    uchar const *ptr = v_ptr;\
    int i;\
    for (i=0; i<num; i++)\
    {\
        if(i != 0)\
        {\
            if(((i%4) == 0) && ((i%16) != 0))\
            {\
                fprintf(fp, "\t");\
            }\
            if((i%16) == 0)\
                fprintf(fp, "\n");\
            fprintf(fp, "%02x", ptr[i]);\
        }\
        else\
            fprintf(fp, "%02x", ptr[i]);\
    }\
    fprintf(fp, "\n");\
}\

Am I not supposed to use void, const or * inside argument list of a macro? Any help would be appreciated.

Upvotes: 0

Views: 98

Answers (3)

Jens Gustedt
Jens Gustedt

Reputation: 78903

macro parameter don't have types. start your macro as

#define hex_display(v_ptr, num)    \

But the main question would be, why you are doing this. In that case a macro has no advantages, only disadvantages:

  • as dbasic mentions in his anwer, you should not evaluate macro arguments twice. In your case you could get away with it by creating two new local variables that you initialize with the macro parameters

  • a function-like macro should syntactically behave exactly like a function call, yours doesn't: put in an if/else or switch could change control flow in a surprising way. The usual trick to avoid that is to put do { ... } while(0) around it.

  • names of local variable may clash with symbols from the outside world

in summary, just don't do it, it is not worth it, in this case. inline your function instead.

Upvotes: 3

vad
vad

Reputation: 344

macros do not require types of inputs --> Remove them

#define hex_display(v_ptr, num)\
{\
    uchar const *ptr = v_ptr;\
    int i;\
    for (i=0; i<num; i++)\
    {\
        if(i != 0)\
        {\
            if(((i%4) == 0) && ((i%16) != 0))\
            {\
                fprintf(fp, "\t");\
            }\
            if((i%16) == 0)\
                fprintf(fp, "\n");\
            fprintf(fp, "%02x", ptr[i]);\
        }\
        else\
            fprintf(fp, "%02x", ptr[i]);\
    }\
    fprintf(fp, "\n");\
}\

Upvotes: 1

doptimusprime
doptimusprime

Reputation: 9395

Macro is a textual substitution, not a function call. It should be

#define hex_display(v_ptr, num)\
{\
    uchar const *ptr = v_ptr;\
    int i;\
    for (i=0; i<num; i++)\
    {\
        if(i != 0)\
        {\
            if(((i%4) == 0) && ((i%16) != 0))\
            {\
                fprintf(fp, "\t");\
            }\
            if((i%16) == 0)\
                fprintf(fp, "\n");\
            fprintf(fp, "%02x", ptr[i]);\
        }\
        else\
            fprintf(fp, "%02x", ptr[i]);\
    }\
    fprintf(fp, "\n");\
}\

Anyway, using macro as a function is not recommended. You should expect under-performance from your debugger. Use some other method to inline.

Why not to use macro, see the following example.

#define text(x) x+x

When you call

 text(i++)

i will be increment twice, instead of once.

Upvotes: 3

Related Questions