why function like macro doesn't work here?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baumann@pan

    #1

    why function like macro doesn't work here?

    hi all,

    i defined a macro like the below


    #define initSock() \
    do{\
    WSADATA ws_data;\
    WSAStartup(0x02 02,&ws_data);\
    }while(0)

    then in a function , i wrote

    if(!initSock())
    {
    .....
    }

    but the compiler complains
    main.c
    e:\project\scan ap\main.c(11) : error C2059: syntax error : 'do'
    e:\project\scan ap\main.c(11) : error C2065: 'ws_data' : undeclared
    identifier
    e:\project\scan ap\main.c(11) : warning C4133: 'function' : incompatible
    types - from 'int *' to 'struct WSAData *'


    if i change the macro to static function, it's ok.


    any help would be appreciated. thanks.

    bauman@Pan

  • Artie Gold

    #2
    Re: why function like macro doesn't work here?

    baumann@pan wrote:[color=blue]
    > hi all,
    >
    > i defined a macro like the below
    >
    >
    > #define initSock() \
    > do{\
    > WSADATA ws_data;\
    > WSAStartup(0x02 02,&ws_data);\
    > }while(0)
    >
    > then in a function , i wrote
    >
    > if(!initSock())
    > {
    > ....
    > }
    >
    > but the compiler complains
    > main.c
    > e:\project\scan ap\main.c(11) : error C2059: syntax error : 'do'
    > e:\project\scan ap\main.c(11) : error C2065: 'ws_data' : undeclared
    > identifier
    > e:\project\scan ap\main.c(11) : warning C4133: 'function' : incompatible
    > types - from 'int *' to 'struct WSAData *'
    >
    >
    > if i change the macro to static function, it's ok.[/color]

    Think of what the compiler `sees' in this case (remember that macros are
    replaced, during the preprocessor phase of compilation, by their expansion).

    [Your compiler may provide a way to see the intermediate, i.e.
    preprocessed, code. Consult its documentation.]

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays

    Comment

    Working...