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

What is the wrong with this code?

P: n/a
#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;
}

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
[code snipped]

Ummmm, not enough comments?

--
Chris.
Nov 15 '05 #2

P: n/a
yezi wrote:
#include <unistd.h> #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


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

http://www.ungerhu.com/jxh/clc.welcome.txt

--
pete
Nov 15 '05 #3

P: n/a
"yezi" <ye*****@hotmail.com> writes:
#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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

P: n/a
On 10 Nov 2005 16:15:02 -0800, "yezi" <ye*****@hotmail.com> wrote:

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>>
Nov 15 '05 #5

P: n/a
On Thu, 10 Nov 2005 18:11:40 -0800, Barry Schwarz <sc******@deloz.net>
wrote in comp.lang.c:
On 10 Nov 2005 16:15:02 -0800, "yezi" <ye*****@hotmail.com> 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]
tlppkt->length =1000;


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.
tlppkt->blankbit = 0;
tlppkt->End=1;


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #6

P: n/a
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

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.