##-concatenation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravi Uday

    #1

    ##-concatenation

    Hi,

    Why is this code not working ?
    It says " 'TestFn_this_is _new_string' : undeclared identifier"

    #define NEW_STR(x) {x##_this_is_ne w_string }

    struct command
    {
    char *name;
    }cm[10] = {NEW_STR(testFn )} ;


    Thx,
    Ravi
  • Joona I Palaste

    #2
    Re: ##-concatenation

    Ravi Uday <ravi368@yahoo. com> scribbled the following:[color=blue]
    > Hi,[/color]
    [color=blue]
    > Why is this code not working ?
    > It says " 'TestFn_this_is _new_string' : undeclared identifier"[/color]
    [color=blue]
    > #define NEW_STR(x) {x##_this_is_ne w_string }[/color]
    [color=blue]
    > struct command
    > {
    > char *name;
    > }cm[10] = {NEW_STR(testFn )} ;[/color]

    Perhaps because you haven't declared the variable
    TestFn_this_is_ new_string anywhere?

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
    - Guardian in "Jinxter"

    Comment

    • Christopher Benson-Manica

      #3
      Re: ##-concatenation

      Ravi Uday <ravi368@yahoo. com> spoke thus:
      [color=blue]
      > Why is this code not working ?
      > It says " 'TestFn_this_is _new_string' : undeclared identifier"[/color]
      [color=blue]
      > #define NEW_STR(x) {x##_this_is_ne w_string }[/color]
      [color=blue]
      > struct command
      > {
      > char *name;
      > }cm[10] = {NEW_STR(testFn )} ;[/color]

      Well, after the macro substitution, you get

      struct command
      {
      char *name;
      } cm[10] = {testFn_this_is _new_string} ;

      First, note that the error message you posted has a capital T, while
      the string you're using for the macro argument has a lowercase t. C
      is case-sensitive, so that could be your problem right there. If not,
      it's probably as Joona suggested - you just didn't declare this
      oddly-named variable anywhere.

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Richard Delorme

        #4
        Re: ##-concatenation

        Ravi Uday a écrit :
        [color=blue]
        > Why is this code not working ?
        > It says " 'TestFn_this_is _new_string' : undeclared identifier"
        >
        > #define NEW_STR(x) {x##_this_is_ne w_string }[/color]

        I think you want this instead:

        #define NEW_STR(x) {#x "_this_is_new_s tring" }

        The following points can explain why the above code works, but not yours:
        - You forgot the quotes to make a valid literal string.
        - You did not use the '#' to turn an identifier into a literal string.
        - Consecutive literal strings are automatically concatenated, so you do
        not need the ## macro operator.
        [color=blue]
        > struct command
        > {
        > char *name;
        > }cm[10] = {NEW_STR(testFn )} ;
        >[/color]

        --
        Richard

        Comment

        Working...