What is the wrong with this code?

Discussion in 'C Programming' started by yezi, Nov 11, 2005.

  1. yezi

    yezi Guest

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <time.h>

    struct nlpPkt
    int source:4; int destin:4;
    unsigned int control:1;
    unsigned int contype:5;
    int length:10;
    int checksum:16;
    char data[1500]; //info holds message/packet if the event type is
    msg/pck arrival.
    struct TLPPKT *tlp;
    } nlpData;

    } NLPPKT; //same size

    struct tlpPkt
    int sequence:7;
    int ack:8;
    int length:10;
    int blankbit:5;
    int End:1;
    char tlpData[1469];

    } TLPPKT;

    int main(){

    NLPPKT *nlppkt;
    TLPPKT *tlppkt;
    char str[8888] = "asbcdefghijklmnopqrstuvwxyz";

    tlppkt=(TLPPKT *)malloc(sizeof(TLPPKT));
    nlppkt=(NLPPKT *)malloc(sizeof(NLPPKT));

    tlppkt->sequence =1;
    tlppkt->ack =2;
    tlppkt->length =1000;
    tlppkt->blankbit = 0;

    memcpy( nlppkt->nlpData.tlp, tlppkt, sizeof(TLPPKT));
    printf("nlppkt.nlpdata.data is %s\n",nlppkt->nlpData.tlp);

    return 0;
    yezi, Nov 11, 2005
    1. Advertisements

  2. [code snipped]

    Ummmm, not enough comments?
    Chris McDonald, Nov 11, 2005
    1. Advertisements

  3. yezi

    pete Guest

    That code has too many headers that I don't have.

    pete, Nov 11, 2005
  4. [snip]

    Please put the question in the body of the article as well as in the
    subject. Not all newsreaders display the subject properly.

    The code is poorly indented, making it difficult to read.

    It casts the result of malloc(). This isn't strictly incorrect, but
    it's unnecessary and can mask certain errors. The quoted lines should

    tlppkt = malloc(sizeof *tlppkt);
    nlppkt = malloc(sizeof *nlppkt);

    It uses several headers that are not defined by the C standard. It
    should be discussed in a system-specific newsgroup, possibly

    When you do post in an appropriate newsgroup, please provide more
    information than just asking what's wrong with the code. You need to
    specify what the code actually does, what you wanted it to do, and how
    those differ.
    Keith Thompson, Nov 11, 2005
  5. There is no question in your message. Some readers cannot see the
    question in your title. Ask your questions in the body of the

    The answer to your question is just about everything.

    You don't tell us what the code is supposed to do or how what it
    actually does differs from the desired result. Only a few here claim
    None standard headers. How do non-unix types know what this is?
    // type comments frequently wrap rendering your code uncompilable.
    I give up. Same size as what?
    You should not cast the return from malloc.
    length is signed and 10 bits wide. The max value it can hold is 511.
    End is signed and 1 bit wide. The max value it can hold is 0.
    tlpData is an array. It cannot appear on the left of an assignment.
    tlp is an uninitialized pointer. You cannot pass an uninitialized
    value to a function.
    When initialized properly, tlp will point to a struct. The first
    member of the struct is not a string.

    <<Remove the del for email>>
    Barry Schwarz, Nov 11, 2005
  6. yezi

    Jack Klein Guest

    It is implementation-defined whether 'length' is signed or unsigned.
    If the OP's implementation makes it unsigned, the maximum value it can
    hold is 1023.
    Ditto, except of course that the maximum value if unsigned is 1.

    6.7.2 Type specifiers P. 5: "Each of the comma-separated sets
    designates the same type, except that for bit-fields, it is
    implementation-defined whether the specifier int designates the same
    type as signed int or the same type as unsigned int."
    Jack Klein, Nov 11, 2005
  7. yezi

    yezi Guest

    I am really new with bit manipulation .Through the comments, I think it
    is the basi bit problem .Would you provide some good websit to read?

    yezi, Nov 11, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.