473,383 Members | 1,853 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,383 software developers and data experts.

Comma operator in a macro-help me understand this plz!!

Dear all,
What does this following piece of code do?
#define CONVERT_SELF_PTR(type,in,out) \
(((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM)

especially can someone let me understand why theres a comma in between

(out) = (type *)(in) and RETURN_OK in the expression ((out) = (type
*)(in), RETURN_OK)?

It will be also helpful if you could let me know the sequence in how
this expression in whole will be evaluated in steps.

Expecting all your help and advanced thanks for the same.

Regards,
s.subbarayan
Nov 14 '05 #1
2 2474
s_**********@rediffmail.com (s.subbarayan) wrote:
#define CONVERT_SELF_PTR(type,in,out) \
(((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM)

especially can someone let me understand why theres a comma in between

(out) = (type *)(in) and RETURN_OK in the expression ((out) = (type
*)(in), RETURN_OK)?
That's just an ordinary comma operator. It works as any other comma
operator does: it separates successive expressions, returning the value
of the last expression as the value of the whole.
It will be also helpful if you could let me know the sequence in how
this expression in whole will be evaluated in steps.


Just peel it off into parts:

(in)!=NULL?
(
out=(type *)in,
RETURN_OK
)
:
ERROR_PARM

That is, it first checks to see whether in is a null pointer. If not, it
casts null to a pointer of type "type" and assigns it to out; and then
evaluates RETURN_OK (which presumably is a single value), and returns
that as the value of the whole expression. If in _is_ a null pointer,
the entire macro evaluates to ERROR_PARM.
No, I can't think of a good reason for writing a macro like that,
either. The context in which you found it might provide an explanation.

Richard
Nov 14 '05 #2
s_**********@rediffmail.com (s.subbarayan) ask:
What does thefollowing piece of code do?

#define CONVERT_SELF_PTR(type,in,out) \
(((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM)


Assuming in particular that RETURN_OK and ERROR_PARAM are distinct
constants, and NULL has its usual meaning, a most natural explanation
is that
- out is expected to be a pointer to type
- in is expected to be a pointer
- the macro checks if in is a non-NULL pointer, and returns a
status accordingly; and in the affirmative only, copies
in to out, with type cast.

The comma is here to execute the assignment and produce RETURN_OK
in the second case; this is done so that the macro behaves
somewhat like a function.
For example

unsigned char *myin;
char *myout;

if (CONVERT_SELF_PTR(char,myin,myout) != RETURN_OK)
printf("error\n");

means

if (myin==NULL)
printf("error\n");
else
myout = (char *)myin;

François Grieu
Nov 14 '05 #3

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

Similar topics

5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
7
by: Koster | last post by:
Hi folks, As I understand it, amongst other things, the comma operator may be used to cause any number of expressions to be evaluated (who's results are thrown away except the last) where only...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
2
by: grid | last post by:
Hi, I need some clarifications on how the comma operator is used to return values from function like macros.I saw a typical implementation as : #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 ,...
1
by: mr.gsingh | last post by:
My program looks something like this: int x = 23; if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x))) { std::cout << "............"; } else { std::cout<< "..............";
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
2
by: Matthias Neubauer | last post by:
Hi! I am a C++ newbie and I'd like to trace the evaluation of some expressions of my programs. I tried to use the comma operator for that, but failed. I used a simple code substitution for...
5
by: bb | last post by:
Hi, Please could someone throw some light on why the following definitions are not the same? class A { const int& a, b; // b does not get defined as a ref. but a plain int
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.