Macro in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andrug
    New Member
    • Mar 2008
    • 1

    Macro in C

    Hello Everybody,

    I have a problem.
    if I have an array of (for example) 50 elements, but i need to write from element numer 100 to 149

    I wanted to define a macro:
    the code I wrote is (in the same file):

    [code=c]
    static array[50]

    #define array[a] array[(a)- 100]


    void main()
    {
    array[101] = 5;
    }
    [/code]

    but it doesn't work.
    Can you please help me?

    Thank a lot!!!!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by andrug
    [code=c]
    #define array[a] array[(a)- 100]
    [/code]
    It wouldn't you have the syntax of your macro definition wrong, a function like macro is defined using parentheses ( and ) so you macro would need to be defined something like

    [code=c]
    #define ARRAY(a) array[(a)- 100]
    [/code]

    Comment

    • telephone
      New Member
      • Mar 2008
      • 3

      #3
      just update the code in the folloing manner,
      static int array[40];
      #define array(x) array[(x)-100]
      void main()
      { array[130]=7;
      }
      I THINK IT WILL BE WORK FINE;

      Comment

      Working...