PDA

View Full Version : How do I concatenate a defines and a string in the C Programming Language



sefs
August 24th, 2009, 01:50 AM
#define CONFIG_PATH "/etc/openvpn/conf"

glob_t gl;

glob("/etc/openvpn/conf/*.conf"), 0, NULL, &gl);


Consider the above. I want to utilize the CONFIG_PATH defines in the glob statement.
How can I do this economically and without reducing performance?

Thanks.

skirkpatrick
August 24th, 2009, 02:09 AM
glob(CONFIG_PATH, 0, NULL, &gl);

The C preprocessor will replace all instances of CONFIG_PATH with it's definition during compilation. As far as the compiler is concerned, there is no difference.

sefs
August 24th, 2009, 02:26 AM
you've lopped off the "/*.conf" part.

How do I get that part in the concatenation.

Thanks.

Habbit
August 24th, 2009, 02:58 AM
In C, a sequence of string literals separated only by whitespace is resolved into a single string literal. For example, "A" "B" resolves to "AB". Thus, you just need to write CONFIG_PATH "/*.conf".

stroyan
August 24th, 2009, 02:59 AM
You can just put two string constants next to eachother to concatenate them.


#define CONFIG_PATH "/etc/openvpn/conf"

glob_t gl;

/* These three lines are equivalent. */
glob("/etc/openvpn/conf/*.conf"), 0, NULL, &gl);
glob("/etc/openvpn/conf" "/*.conf"), 0, NULL, &gl);
glob(CONFIG_PATH "/*.conf"), 0, NULL, &gl);

lensman3
August 24th, 2009, 03:32 AM
Look at the -E option for gcc. The -E option will only invoke the pre-processor so you can then look at the output of #define catenation.

This also shows all the includes and how they are modified and added. Your code will be at the very end.

sefs
August 24th, 2009, 03:38 AM
good show all. Thanks for that!