macro for compile time allocation

Discussion in 'C Programming' started by moose, May 18, 2005.

  1. moose

    moose Guest

    Can someone explain why the following will not work,

    #define MYSTRUCT(name,size) struct my_struct_ ## name {
    unsigned char name ## _array[ size ] ;
    } ;

    MYSTRUCT( foo , 10 )

    The error is because the size doesnt appear to be known at compile time
    to create the array but why not ? this is a macro not a function.

    Any comments welcome
    moose, May 18, 2005
    #1
    1. Advertising

  2. moose

    pete Guest

    moose wrote:
    >
    > Can someone explain why the following will not work,
    >
    > #define MYSTRUCT(name,size) struct my_struct_ ## name {
    > unsigned char name ## _array[ size ] ;
    > } ;


    You need two backslashes.

    #define MYSTRUCT(name,size) struct my_struct_ ## name {\
    unsigned char name ## _array[ size ] ;\
    } ;

    --
    pete
    pete, May 18, 2005
    #2
    1. Advertising

  3. moose

    CBFalconer Guest

    pete wrote:
    > moose wrote:
    >>
    >> Can someone explain why the following will not work,
    >>
    >> #define MYSTRUCT(name,size) struct my_struct_ ## name {
    >> unsigned char name ## _array[ size ] ;
    >> } ;

    >
    > You need two backslashes.
    >
    > #define MYSTRUCT(name,size) struct my_struct_ ## name {\
    > unsigned char name ## _array[ size ] ;\
    > } ;


    You might tell him why. #defines have to be complete on one line.
    To write them on multiple lines you have to escape the line
    endings, which means an escape char (the \) at the end of each
    continued line. Nothing follows the escape except the eoln marker.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, May 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    24
    Views:
    3,842
    Ben Bacarisse
    Nov 30, 2006
  2. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  3. Nagaraj
    Replies:
    1
    Views:
    841
    Lionel B
    Mar 1, 2007
  4. Bin Chen

    Increment a number in compile time macro

    Bin Chen, Sep 7, 2008, in forum: C Programming
    Replies:
    9
    Views:
    625
    Harald van Dijk
    Sep 8, 2008
  5. Carter
    Replies:
    2
    Views:
    490
    Carter
    Mar 4, 2009
Loading...

Share This Page