473,385 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why function like macro doesn't work here?

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

Nov 15 '05 #1
8 2646
"baumann@pan" <as*******@hotmail.com> writes:
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.


The do { ... } while(0) macro definition trick lets you use a macro
invocation as if it were a function call, but only in a statement
context. It doesn't let you use it in an expression, because you
can't have a loop in an expression.

Even if it were allowed, your code probably wouldn't work. You're
trying to use the value "returned" by initSock(), but it doesn't yield
a value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #2


Keith Thompson wrote:
"baumann@pan" <as*******@hotmail.com> writes:
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.


The do { ... } while(0) macro definition trick lets you use a macro
invocation as if it were a function call, but only in a statement
context. It doesn't let you use it in an expression, because you
can't have a loop in an expression.

Even if it were allowed, your code probably wouldn't work. You're
trying to use the value "returned" by initSock(), but it doesn't yield
a value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

is there the way to solute it by macro?

Nov 15 '05 #3
baumann@pan wrote:
>
> 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())
is there the way to solute it by macro?


I don't think so; but why would you bother?

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #4


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())
{
....
}

Whoa, hold on. Remeber that a macro is a text placeholder *only*;
after preprocessing, the above statement gets expanded to

if (!do{WSADATA ws_data;WSAStartup(0x0202,&ws_data);}while(0))

Hence the following:
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.
Don't do it as a macro. That's about the only help I can offer.

bauman@Pan


Nov 15 '05 #5
Me
baumann@pan wrote:
#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.


"if" wants an expression that returns non-void, you're passing a
statement (minus the required semi-colon) to it. You have a few ways to
convert this to an expression (I'm assuming the return of WSAStartup is
the thing you want to test):

1. rewrite this macro:
#define initSock(v) ... v = WSAStartup ...

some_t good;
initSock(v);
if (v)
...

2. GNU C expression statement extension:
basically replace the do with ( and replace the while(0) with ).

3. rewrite it as a function:
you can figure this out

4. C99 compound literals:
#define initSock() WSAStartup(0x0202, &(WSADATA){})

Nov 15 '05 #6
"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.

any help would be appreciated. thanks.


If I declare initSock() as a function:

void initSock()
{
do{
WSDATA ws_data;
WSAStartup(0x0202,&ws_data);
}while(0);
}

my compiler complains here as well, telling me on the "if(!initSock())"
line:

foo.c(20) : error C2171: '!' : illegal on operands of type 'void '
foo.c(20) : error C2180: controlling expression has type 'void'
foo.c(20) : error C2180: controlling expression has type 'void'

Hint... what will be the result of expanding your macro?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #7
On 8 Jul 2005 13:01:46 -0700, "Me" <an*****************@yahoo.com>
wrote:

<snip>
"if" wants an expression that returns non-void, you're passing a
statement (minus the required semi-colon) to it. You have a few ways to
Actually, that yields scalar = not void and not struct or union;
arithmetic or pointer is OK, or array which decays to pointer.
convert this to an expression (I'm assuming the return of WSAStartup is
the thing you want to test):

1. rewrite this macro:
#define initSock(v) ... v = WSAStartup ...

some_t good;
initSock(v);
if (v)
...
initSock(good);
if( good )

except that actually WSAStartup returning 0 is good and nonzero is
bad, so either
#define initSock(ok) ... ok = WSAStartup (...) == 0; ...
_Bool myok; initSock (myok); if( !myok ) /* error */

or
#define initSock(err) ... err = WSAStartup (...); ....
int myerr; initSock (myerr); if( myerr /* != 0 */ ) /* error */
2. GNU C expression statement extension:
basically replace the do with ( and replace the while(0) with ).

3. rewrite it as a function:
you can figure this out

4. C99 compound literals:
#define initSock() WSAStartup(0x0202, &(WSADATA){})


or even in C90 ... WSAStartup (0x0202, malloc(sizeof(WSADATA))) ...
leaving one fairly small memory leak per process.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #8
On 2005-07-07 21:27:51 -0500, "baumann@pan" <as*******@hotmail.com> said:
#define initSock() \
do{\
WSADATA ws_data;\
WSAStartup(0x0202,&ws_data);\
}while(0)

then in a function , i wrote

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

Why don't you use

inline int initSock()
{
....
}
if i change the macro to static function, it's ok.


I make it easy: think the #define statement as just as a
Search-and-Replace facility given by the compiler.

--
Sensei <se******@tin.it>

cd /pub
more beer

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard Hollenbeck | last post by:
Back in the old days before I started learning about menubars (the day before yesterday,) I based all my operations on command buttons on the forms. Now the forms are all cluttered up with buttons...
13
by: Shi Jin | last post by:
Hi there, While I was trying to disassemble a simple C program for my OS course, I found that the call to function longjmp is actually called to siglongjmp under linux. And similarly, under...
26
by: GS | last post by:
I am doing my first real embedded programming project and the supplied device libraries have a function definition that looks like this: void FCN(void) = { INTDEF, INTABS, (unsigned short)...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
1
by: baumann | last post by:
hi all, i defined a macro like the below #define initSock() \ do{\ WSADATA ws_data;\ WSAStartup(0x0202,&ws_data);\ }while(0)
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
7
by: hopangtsun | last post by:
Hi all, I have encountered a problem on using the function template my goal is to add two numbers, which they can be int or double if I do this in this way template<class T> T addition(T a, T...
45
by: madhawi | last post by:
Is it better to use a macro or a function?
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.