what does this code do?

Discussion in 'C Programming' started by qianz99@gmail.com, Mar 14, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. Eric Sosman Guest

    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]


    --
    Eric Sosman, Mar 14, 2008
    #2
    1. Advertising

  3. Guest

    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:
    > 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]

    >
    > --
    >
    , Mar 14, 2008
    #3
  4. Eric Sosman Guest

    Putting the response before the stimulus.
    What is "top-posting?"
    Please don't top-post.

    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?


    You seem not to understand what preprocessor macros are.
    I suggest you find a good C textbook or tutorial and start
    reading it. Usenet is a fine medium for focused questions
    and discussions (and flames), but is not effective as a means
    for mass transfer of basic knowledge.

    --
    Eric Sosman, Mar 14, 2008
    #4
  5. writes:

    Please don't "top post". I've fixed it by moving your comments below
    the text they refer to. I've also snipped some text as you should
    have done...

    > Eric Sosman wrote:
    >> 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.

    >
    > 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?


    No, the code just defines some macros that seem not to be used. The
    switch has only one case -- the default one.

    You will have to read up on what #define does, but one of the macros
    (PDU) is was obviously deigned to allow switch cases to be added. It
    looks odd that it is not used, so I searched for similar code and
    found a version of what you posted with one crucial difference:

    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;
    #include "smpp_pdu.def"
    default:
    error(0, "Unknown SMPP_PDU type, internal error.");
    gw_free(pdu);
    return NULL;
    }

    Just before the default case, the file "smpp_pdu.def" is included.
    This file uses all the macros defined here to add cases to the switch
    statement. Obviously the code you posted is designed with this
    possibility in mind.

    --
    Ben.
    Ben Bacarisse, Mar 14, 2008
    #5
  6. CBFalconer Guest

    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
    CBFalconer, Mar 14, 2008
    #6
    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. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,210
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Bill Johnson
    Replies:
    0
    Views:
    1,232
    Bill Johnson
    Jul 8, 2005
  3. keithb
    Replies:
    1
    Views:
    905
    Bruce Barker
    Mar 29, 2006
  4. Nick L
    Replies:
    10
    Views:
    589
    Jerry Coffin
    Aug 31, 2004
  5. jblazi
    Replies:
    5
    Views:
    427
    jblazi
    Aug 16, 2004
Loading...

Share This Page