What is the wrong with this code?

Y

yezi

#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;
union
{
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;
tlppkt->End=1;
tlppkt->tlpData=str;

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

return 0;
}
 
K

Keith Thompson

yezi said:
#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> [snip]
tlppkt=(TLPPKT *)malloc(sizeof(TLPPKT));
nlppkt=(NLPPKT *)malloc(sizeof(NLPPKT));
[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
be:

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
comp.unix.programmer.

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.
 
B

Barry Schwarz

There is no question in your message. Some readers cannot see the
question in your title. Ask your questions in the body of the
message.

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
clairvoyance.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

None standard headers. How do non-unix types know what this is?
#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;
union
{
char data[1500]; //info holds message/packet if the event type is
msg/pck arrival.

// type comments frequently wrap rendering your code uncompilable.
struct TLPPKT *tlp;
} nlpData;

} NLPPKT; //same size

I give up. Same size as what?
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));

You should not cast the return from malloc.
nlppkt=(NLPPKT *)malloc(sizeof(NLPPKT));

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

length is signed and 10 bits wide. The max value it can hold is 511.
tlppkt->blankbit = 0;
tlppkt->End=1;

End is signed and 1 bit wide. The max value it can hold is 0.
tlppkt->tlpData=str;

tlpData is an array. It cannot appear on the left of an assignment.
memcpy( nlppkt->nlpData.tlp, tlppkt, sizeof(TLPPKT));

tlp is an uninitialized pointer. You cannot pass an uninitialized
value to a function.
printf("nlppkt.nlpdata.data is %s\n",nlppkt->nlpData.tlp);

When initialized properly, tlp will point to a struct. The first
member of the struct is not a string.
return 0;
}


<<Remove the del for email>>
 
J

Jack Klein

On 10 Nov 2005 16:15:02 -0800, "yezi" <[email protected]> wrote:

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

// type comments frequently wrap rendering your code uncompilable.
struct TLPPKT *tlp;
} nlpData;

} NLPPKT; //same size

[snip]
length is signed and 10 bits wide. The max value it can hold is 511.

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.
End is signed and 1 bit wide. The max value it can hold is 0.

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."
 
Y

yezi

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?

Thanks
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top