-4

Having this statement:

dist[i]= min(dist[i],dist[j]+edge[j][i]);

How safe the corresponding following macro (in C) is:

#define MIN(d[i], edge[i][j]) (d[i] < edge[i][j] ? (d[i]) : (edge[i][j]))

Thank you.

  • 1
    Perfectly safe, because you aren't using it. But why not write a function? – juanchopanza Oct 10 '15 at 15:06
  • 2
    Does that even compile? – Nikolai Ruhe Oct 10 '15 at 15:09
  • 1
    That's not a proper macro definition. Arguments to a macro should be simple identifiers, not array references. – lurker Oct 10 '15 at 15:10
  • No, it gives me an error that why I am posting this because I thought it includes a side effects – Jackson Arms Oct 10 '15 at 15:10
  • BTW, it is a FAQ. See [this](http://stackoverflow.com/a/3437484/841108) – Basile Starynkevitch Oct 10 '15 at 15:11
  • 1
    *I thought it includes side effects* doesn't really explain why you might have array references as arguments to a macro. Surely, macros can have side effects. But that has nothing to do with argument declaration syntax. You really should look at C documentation for how macros work. Things don't work just like you think they might. – lurker Oct 10 '15 at 15:16
  • @JacksonArms What do you think macros do? From the looks of it, you might have some conceptual problem. – PC Luddite Oct 10 '15 at 16:40

1 Answers1

1

If you want a Min definition macro you need to do it like that

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

Like said in comment, macros need identifiers and not array, not even variables. But this macros is note type safe. Why not using fmax and fmin? It's better and you dont have to create a macro

Captain Wise
  • 480
  • 3
  • 13