Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why function like macro doesn't work here?

Reply
Thread Tools

why function like macro doesn't work here?

 
 
baumann@pan
Guest
Posts: n/a
 
      07-08-2005
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      07-08-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inline function vs function-like macro subramanian100in@yahoo.com, India C Programming 2 03-06-2007 04:43 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
why function like macro doesn't work here? baumann@pan C Programming 8 07-15-2005 04:22 PM



Advertisments