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]);
2
  • 1
    Can you give an example of how you want the macro to be used? Maybe a code snippet? Commented Dec 21, 2012 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 Commented Dec 21, 2012 at 12:38

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Ternary operator. When abuse of macros is simply not enough.
@LihO : What about second way?
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 : 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!
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 :)
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.

2 Comments

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.
@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.
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.

Comments

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]);

5 Comments

thanks for the explanation. Could you please also give an exp for the solution that u mentioned. Thanks
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 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.
@izomorphius: Code that is possible to compile and code that is syntactically legal are 2 different things.
@LihO point taken! I have changed my wording to reflect what I meant.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.