-1

I have a structure A such that A has fields v0 and v1. There is an array of A's:

A St[3];

I'd like to define B like that:

#define B[x] St[x].v0 

I can't insert x as a parameter to define. Is there some way to do that in C? My purpose is to be able to replace each instance of St[x].v0 with B[x].

mrflash818
  • 930
  • 13
  • 24
Igor D
  • 23
  • 1
  • 2
    Instead of describing your code, please show it. Try to create a [mcve] that you show us in the question. – Some programmer dude May 02 '19 at 10:55
  • Why though? It serves little purpose beyond winning in the ioccc. – StoryTeller - Unslander Monica May 02 '19 at 10:58
  • It will minimise code changes in an existing project – Igor D May 02 '19 at 11:01
  • 3
    You can do it if you use parentheses instead of square brackets: `#define B(x) St[x].v0`. – Ian Abbott May 02 '19 at 11:03
  • 1
    `replace each instance of St[x].v0 with B[x]` - the macro you provided `#define B[x] St[x].v0 ` would substitute `B[x]` for `St[x].v0` (if only `[` would allowed inside a macro name). Please show usage example of `St[x].v0` or `B[x]`.Anyway, lesson for today: write proper abstraction and use accesor function. – KamilCuk May 02 '19 at 11:03
  • You can do `#define B(x) St[x].v0 ` – Paul Ogilvie May 02 '19 at 11:03
  • Does it mean that I can still access B as an array? – Igor D May 02 '19 at 11:04
  • 1
    Not strictly as an array, but you can access the elements like this: `B(i) = 42;` or `a = B(j);`. – Ian Abbott May 02 '19 at 11:07
  • 2
    _"It will minimise code changes in an existing project"_ - can you provide us with more information? What you're doing does not appear to be the optimal solution for minimizing code changes... – andreee May 02 '19 at 11:08
  • "My purpose is to be able to replace each instance of St[x].v0 with B[x]." But your code rather attempts the opposite, to replace B[x] with St[x].v0. And therefore every posted answer does too. What do you actually want? – Lundin May 02 '19 at 11:29
  • Voting to close this as unclear for now. Every posted answer is possibly wrong, regardless of the [] vs () issue. – Lundin May 02 '19 at 11:30
  • By the way, while macros might seem nice for things like this, in the long run it actually makes your code harder to read and understand, and therefore also harder to maintain. Macros, especially function-like macros or macros that somehow rename or change standard semantics, should be avoided as much as possible. – Some programmer dude May 02 '19 at 11:33
  • 1
    @Someprogrammerdude: Do not ask for a MCVE as a knee-jerk response. MCVEs are for asking about code that is not working. This question does not do that, and it is perfectly clear without a MCVE. The OP wants `#define B(x) …` with brackets instead of parentheses. The answer is C does not do that, no MCVE needed. – Eric Postpischil May 02 '19 at 12:08

3 Answers3

3

A macro that substitutes parameters is called a function-like macro, and the C standard only provides for function-like macros using parentheses, not brackets.

You can use below method:

#define B(x) St[x].v0 
Vagish
  • 2,520
  • 19
  • 32
  • 1
    You write that “`#define` does not support array/bracket operator,” but then you show a `#define` that uses an “array/bracket” operator (properly called a *subscript operator*). I know what you mean, but people who are learning these things may not. Answers need to use language that explains clearly to people who do not yet understand. A macro that substitutes parameters is called a *function-like macro*, and the C standard only provides for function-like macros using parentheses, not brackets. – Eric Postpischil May 02 '19 at 13:28
2

According to the C reference, the defintion of #define is

#define <identifier>[( parameters, ... )] [replacement-list]

You must not use square brackets within an <identifier> (see C99 standard) and therefore your suggested solution would not be properly processed by the preprocessor.

What would work instead is

#define B(x) St[x].v0 // note the brackets

where you would pass additional parameters to the macro (using parentheses). However this will obfuscate your code and most probably confuse users/readers of your code. See also why you should be careful with macros.

andreee
  • 4,459
  • 22
  • 42
1

You can have

#define B(x) St[x].v0

ant then replace each instance of St[x].v0 with B(x).

I'm afraid it's impossible to do it with [], you'd need operator overloading (like the one in C++) for that.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142