why function like macro doesn't work here?

Discussion in 'C Programming' started by baumann@pan, Jul 8, 2005.

  1. baumann@pan

    baumann@pan Guest

    hi all,

    i defined a macro like the below


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

    then in a function , i wrote

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

    but the compiler complains
    main.c
    e:\project\scanap\main.c(11) : error C2059: syntax error : 'do'
    e:\project\scanap\main.c(11) : error C2065: 'ws_data' : undeclared
    identifier
    e:\project\scanap\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
     
    baumann@pan, Jul 8, 2005
    #1
    1. Advertising

  2. baumann@pan

    Artie Gold Guest

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


    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
     
    Artie Gold, Jul 8, 2005
    #2
    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. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    500
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,221
    Smokey Grindel
    Dec 2, 2006
  3. baumann@pan

    why function like macro doesn't work here?

    baumann@pan, Jul 8, 2005, in forum: C Programming
    Replies:
    8
    Views:
    491
    Sensei
    Jul 15, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    501
    Patrick Kowalzick
    Mar 14, 2006
  5. , India

    inline function vs function-like macro

    , India, Mar 6, 2007, in forum: C Programming
    Replies:
    2
    Views:
    662
    Flash Gordon
    Mar 6, 2007
Loading...

Share This Page