Craig Sparks
Craig Sparks

Reputation: 23

Looping thru defines

Say I have a list of defines MC_SERVER1, MC_SERVER2, MC_SERVER3 how would I loop thru to get the contents of each. Also I do not know how many I will have. I might have 3 or 10. This is for C programming

Say I have DEFINE MC_SERVER1="mc1.sdsds.com" DEFINE MC_SERVER2="mc2.sdsds.com"

how do I loop thru them all.

Upvotes: 0

Views: 103

Answers (3)

blueshift
blueshift

Reputation: 6882

You can do this if they are in an array, but not just with a list of defines as you have.

Such as:

const char *servers[] = {
  "server.one",
  "server.two",
};

If you're interested in this approach, also see how to get the length of an array at compile time

Upvotes: 0

vsz
vsz

Reputation: 4909

There is a possibility to obtain similar effects with macros, through tricks with concatenation (##) and stringification (#), but it would probably be better for you to just don't use macros.

See Variable names in C for examples.

Upvotes: 0

SuperRod
SuperRod

Reputation: 557

Defines don't work that way. They are evaluated at compile time, not execution time, so instances of them are replaced with their values.

Upvotes: 3

Related Questions