Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid conflict between the definition of a function and macro with the same name?

I’ve defined a function and a macro like this to set the values inside of a matrix:

void init_matrix(Matrix *mat, double ea[],  size_t size)
{
    for (int i = 0; i < mat->row; i++)
        for (int j = 0; j < mat->col; j++) {
            if (!(i*mat->col+j<size))
                return;
            mat->entries[i][j] = ea[i*mat->col + j];    
        }
}

#define init_matrix(mat, ...) init_matrix(mat, (double []) {__VA_ARGS__}, sizeof((double []) {__VA_ARGS__})/sizeof(double))

It works fine now, but I was considering moving the function declarations and macros into a header file and then including that header file into this program. But when I do that, it’ll probably make expand the definition of the function into something like:

void init_matrix(Matrix *mat, (double []) {double ea[], size_t size}, sizeof((double []) {double ea[], size_t size})/sizeof(double))

Which would mess everything up, right? How can I avoid that?
Maybe putting this function at the end of the file and using an #undef init_matrix before it? Or is there a way to undefine a macro just in a section of the code and keep it working like it did before before and after that part?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Put parentheses around your function name:

void (init_matrix)(Matrix *mat, double ea[],  size_t size)
{
    ...
}

and around the function name in the prototype if you are using a header:

void (init_matrix)(Matrix *, double[],  size_t);

and you are done.

Take a look to What do the parentheses around a function name mean? for more info.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading