Jump to content
  • Advertisement
Sign in to follow this  
DrTwox

Using strings in macros

This topic is 3511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. I'm trying to create a macro in C...
#define DB_QUERY_ALBUMS(a) "SELECT DISTINCT album FROM music WHERE artist=\""#a"\" ORDER BY year;"
Where (a) is the name of the artist to substitute into the macro. I call the macro like this: list = db_query(DB_QUERY_ALBUMS_BY_ARTIST(Ministry)); Using "gcc -E" I can see the macro expands to: "SELECT DISTINCT album FROM music WHERE artist=\"""Ministry""\" ORDER BY year; Which is obviously wrong, as there are too many quotes! If I drop the hash from the macro it expands to: "SELECT DISTINCT album FROM music WHERE artist=\""Ministry "\" ORDER BY year;" ...still too many quotes, and a null byte terminator is added. I've tried reading a few online texts about the C preprocessor, but they seem to jump from newbie programmer to hardcore macro-foo without any steps in between! What's the correct way of doing this?

Share this post


Link to post
Share on other sites
Advertisement
What you've already got should just work. It doesn't matter that you've got too many quotes, since adjacent string literals in C will automatically concatenate.

eg. "abc" "def" is the same as "abcdef".

Share this post


Link to post
Share on other sites
As Sc4Freak proposed, try:

#define DB_QUERY_ALBUMS(a) "SELECT DISTINCT album FROM music WHERE artist=\"" a "\" ORDER BY year;"

list = db_query(DB_QUERY_ALBUMS_BY_ARTIST("Ministry"));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!