By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,594 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,594 IT Pros & Developers. It's quick & easy.

what does this code do?

P: n/a
Hi I am not sure what this code does.
I have the following questions
1. where is the case?
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define

3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"

Thank you very much!
switch (type) {
#define OPTIONAL_BEGIN
#define TLV_INTEGER(name, octets) p->name = -1;
#define TLV_NULTERMINATED(name, max_len) p->name = NULL;
#define TLV_OCTETS(name, min_len, max_len) p->name = NULL;
#define OPTIONAL_END
#define INTEGER(name, octets) p->name = 0;
#define NULTERMINATED(name, max_octets) p->name = NULL;
#define OCTETS(name, field_giving_octetst) p->name = NULL;
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;
default:
error(0, "Unknown SMPP_PDU type, internal error.");
gw_free(pdu);
return NULL;
}
Mar 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
qi*****@gmail.com wrote:
Hi I am not sure what this code does.
It switches on `type', goes to the default: label,
calls error() and gw_free(), and returns NULL from the
function that contains it.
I have the following questions
1. where is the case?
The only case in this switch statement is the default.
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define
#define defines a preprocessor macro.
3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"
Each backslash-newline pair ends a line of source code
but does not end the macro definition (without the backslash,
the macro definition would end at the newline). So the body
of the PDU macro contains not only what's on the #define line
itself, but all of the following seven lines as well. The
definition ends after the eighth line because the newline
at the end of that line is not preceded by a backslash.

As for #name, see Question 11.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list <http://www.c-faq.com/>.
[code snipped; see up-thread]
--
Er*********@sun.com
Mar 14 '08 #2

P: n/a
Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question.
If switch only goes to the default value, why there are so many
#define in front of the default value. Does it mean it goes to the
macros one by one?

Thank you very much!

Eric Sosman wrote:
qi*****@gmail.com wrote:
Hi I am not sure what this code does.

It switches on `type', goes to the default: label,
calls error() and gw_free(), and returns NULL from the
function that contains it.
I have the following questions
1. where is the case?

The only case in this switch statement is the default.
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define

#define defines a preprocessor macro.
3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"

Each backslash-newline pair ends a line of source code
but does not end the macro definition (without the backslash,
the macro definition would end at the newline). So the body
of the PDU macro contains not only what's on the #define line
itself, but all of the following seven lines as well. The
definition ends after the eighth line because the newline
at the end of that line is not preceded by a backslash.

As for #name, see Question 11.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list <http://www.c-faq.com/>.
[code snipped; see up-thread]

--
Er*********@sun.com
Mar 14 '08 #3

P: n/a
qi*****@gmail.com wrote:
>
Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question. If switch only goes to
the default value, why there are so many #define in front of the
default value. Does it mean it goes to the macros one by one?
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)
--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.