Re: pls expand this macro



sathyashrayan wrote:
 /*
**  PLURALTX.C - How to print proper plurals
**  public domain - original algorithm by Bob Stout
*/


#include <stdio.h>


#define plural_text(n) &"s"[(1 == (n))]

This is equivalent to ( "s" + (1 == (n) ) )
ie. adding 0 or 1 to the starting address of the string literal "s". If adding zero, the result is a pointer to the string "s". If adding one, the result is a pointer to the string "".


#define plural_text2(n) &"es"[(1 == (n))<<1]

This is equivalent to ( "es" + (1 == (n)) * 2 )
ie. adding 0 or 2 to the starting address of the string literal "es". If adding zero, the result is a pointer to the string "es". If adding two, the result is a pointer to the string "".


int main(void)
{
  int i;

  for (i = 0; i < 10; ++i)
    printf("%d thing%s in %d box%s\n", i, plural_text(i),
> i, plural_text2(i));
  return 0;
}

My questions:

1)  I try to understand the two macros plural_text and plural_text2.
array subscripting is commutative

That's irrelevant. This code uses array subscripting in the usual way, with the array on the left, and the subscript between the brackets.


so in the above we could expand the macros as plural_text n[1] = "s" .

No. Not at all. The invocation plural_text(i) expands to the following eleven tokens: & "s" [ ( 1 == ( i ) ) ]

The parenthesis around
the variable (n) since the macros does not know the type of the variable
it is acting on. Am I correct?

No. It acts on an expression, not a variable. The parenthesis are in case the expression given contains other operators that may affect the parsing.


2) I don't able to understand the use of & in both of the macros. Does
it used for the rule "array decays into
the pointer to it's first element"?

The & operator is not applied to the array. It is applied to the expression containing the subscript. That is, it is parsed as
& ( "s"[(1 == (i))] )
rather than
(& "s") [(1 == (i))]


Here's one way to understand it: "s" is an array of two char. That array is subscripted, to give an lvalue referring to either the first or second element of the array, and then the address-of operator is applied to the lvalue, giving a pointer to the first or second element of the array.

3) << operator in the macro plural_text2 used to move the string to the
second one, in the above case
it is "s". Correct?

The << operator is a binary shift on an integer value. Shifting left by one bit is equivalent to multiplying by two. It has the effect of changing a value that could be 0 or 1, into a value that could be 0 or 2 respectively.


--
Simon.
.



Relevant Pages

  • Re: array subscript type cannot be `char`?
    ... int main ... "For an array of arrays, the permitted pointer arithmetic in subclause ... "An array subscript is out of range, even if an object is apparently ...
    (comp.lang.c)
  • Re: Can a static array contain a dynamic array of pointers?
    ... > function should receive a pointer to the struct... ... When you declare a function parameter as an array it actually declares ... array -- or actually any element such that the subscript is within the ... "decays" into a pointer to its first element ...
    (comp.lang.c)
  • Re: hacker challenge - traverse a list
    ... finding t, a "temporary pointer", using a binary tree search. ... that there is no magic that allows you to find the subscript when you ... are at an array entry in some situation where you have forgotten it: ...
    (comp.programming)
  • Re: Can I get COBOL to look in a mirror?...
    ... MyMirrorImage1 would have its own storage block for a single copy ... > array, which I can use it as subscript or a pointer, to uniquely ...
    (comp.lang.cobol)
  • Re: use of "->" on arrays?
    ... > int a; ... It's the base address of the array. ... Following is not allowed as the operator -> expects a pointer. ... to second element of the array. ...
    (comp.lang.c)