简体   繁体   中英

Why won't this swap macro compile?

The K&R 2nd ed answer book has the following solution for a variable argument swap macro

#define swap(t, x, y) { t _z; \   
                       _z = y; \ 
                       y = x; \  
                       x = _z; } 

Visual Studio Express is telling me that y and x must be constant values and that there is an "expected declaration" to the left of the right brace.

Is this formatting out of date?

Edit:

The way that code is formatted won't let me compile for the errors listed above, but the following code seems to be fine:

#define swap(t, x, y) { t _z; _z = y; y = x; x = _z; }

The problem is you have whitespace after the \\ line continuation characters. As such, the \\ is no longer treated as a line continuation, but just a non-whitespace character. This means the following lines are not part of the macro definition, and the compiler tries to compile them as normal lines. Getting rid of the whitespace after each \\ will fix the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM