1

I would like to take an array of strings in a macro. Firstly: is that possible?

IF yes, can I call them one by one based on the index, when I am using them?

Something like this:

#define VAR    "abc", "def", "xyz"

Then when i want to use "def" somewhere,

FUNC(VAR[1]);
Sunny
  • 7,444
  • 22
  • 63
  • 104
  • 1
    Can you give an example of how you want the macro to be used? Maybe a code snippet? – Mark Byers Dec 21 '12 at 12:33
  • I have edited the main question. I would like something like this? is it possible, if not then what would be the nearest possible solution?Thanks – Sunny Dec 21 '12 at 12:38

4 Answers4

2

May be my code helpful to you:

#include<stdio.h>
#include<stdlib.h>
#define STRING_ARRAY  "ONE", "TWO", "THREE", "NULL"

int main(){

    char* STRING[] = {STRING_ARRAY};

    int i=0;
    scanf("%d",&i);
    printf("%s\n",STRING[i]);
    return EXIT_SUCCESS;
}

This also works:

:~$ gcc x.c  -o  x
:~$ ./x
1
TWO
e:~$ ./x
2
THREE  

You have to change in MACRO only in at re-compilation.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • **Ternary operator**. When abuse of macros is simply not enough. – LihO Dec 21 '12 at 13:11
  • @LihO : What about second way? – Grijesh Chauhan Dec 21 '12 at 13:20
  • Much better. But the thing is that the only purpose of this macro in this case is to replace line `char* STRING[] = {STRING_ARRAY};` with `char* STRING[] = {"ONE", "TWO", "THREE", "NULL"};` before this source file is compiled, which makes that macro a bit useless. – LihO Dec 21 '12 at 15:44
  • 1
    @LihO : Liho what I understood that use of macro is we able to change at one place instead of every where in file. I that way it con be useful...and I think I shouldn't delete my answer. Should I? ...Thanks Liho! – Grijesh Chauhan Dec 21 '12 at 15:51
  • Actually, this reminds defining a simple constant, which is definitely not a bad practice at all. If you know that there is string literal that you're going to use at more places, it's much better idea to use `define` rather than copy-paste it :) – LihO Dec 21 '12 at 15:56
1
#define VAR(...) const char *FOO[] = { __VA_ARGS__ }
...
VAR("abc", "xyz");
printf("%s\n", FOO[0]);

But note:

  1. Macros are evil.
  2. Declare a variable in macro is the worst idea ever.
maverik
  • 5,508
  • 3
  • 35
  • 55
  • 1
    I subscribe to the second point :) But it would be really important to declare the variable as `char const* []` at least. And then as you do it, yes this is evil, one should never do this like that. Provide the name of the variable as an argument. – Jens Gustedt Dec 21 '12 at 15:10
  • @JensGustedt, thanks, I changed to the `const char *`. Don't know how to be with the name, OP wants just a macro that receives strings as parameters. There can be a misunderstanding, I think. – maverik Dec 21 '12 at 15:16
1

Starting with C99 you can use compound literals

#define VAR ((char const*[]){ "abc", "def", "xyz" })

and then use this as VAR[2] or so.

A compound literal is similar to a cast of an initializer, here this is an array of base type char const*. The const is important such that you don't inadvertently try to modify the string literals.

Any modern compiler should be able to fold all different occurrences of that array and of the string literals into one single instance.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

The macro will expand to the text on the right so try to answer your question for yourself.

Here is a way to try to understand macro-s:

FUNC(VAR[1]); <=> FUNC("abc", "def", "xyz"[1]);

Will the one on the right do what you expect? No? So then you can't use it like this. However you can use this for static array initilization and then access the array by index for instance.

EDIT: here is how I propose you to use the MACRO:

char* a[] = {VAR};
FUNC(a[0]);
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • thanks for the explanation. Could you please also give an exp for the solution that u mentioned. Thanks – Sunny Dec 21 '12 at 12:43
  • 1
    Actually the one on the right is perfectly legal C++ syntax as far as I can tell - it just doesn't do what Sunny wants. – sepp2k Dec 21 '12 at 12:49
  • @sepp2k it depends on the arguments that FUNC takes, but I imagine it is expecting a single string so probably this code will fail to compile. – Ivaylo Strandjev Dec 21 '12 at 12:50
  • @izomorphius: Code that is possible to compile and code that is syntactically legal are 2 different things. – LihO Dec 21 '12 at 12:58
  • @LihO point taken! I have changed my wording to reflect what I meant. – Ivaylo Strandjev Dec 21 '12 at 13:03