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

struct

P: n/a
Hi,
I made a simple struct and 'm trying to assign value to its members
but nothing is being actually assigned (I tested it by printing it...)

code :

struct header{
unsigned char msgtype;
unsigned char uoid[20];
unsigned char ttl;
unsigned char reserved;
unsigned int datalength;
};


struct header h;
h.msgtype=((unsigned char)0xFA);
cout<<"size "<< sizeof h<<endl; //prints "28"
unsigned char buf[20];
memset(h.uoid, 0, 20);
memcpy(h.uoid,(unsigned char *)GetUOID((char *)node_instance_id,
"hello", buf, sizeof buf),20); //GetUOID is func
h.ttl=1; //prints nothing
cout<<"TTL "<<h.ttl<<endl;
h.reserved=0;
h.datalength=strlen(myhostname)+sizeof (myport);

any idea what's wrong???
Oct 15 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Neel wrote:
>
any idea what's wrong???
You're posing C++ to a C group?

--
Ian Collins
Oct 15 '08 #2

P: n/a
On Oct 15, 2:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
Neel wrote:
any idea what's wrong???

You're posing C++ to a C group?

--
Ian Collins
C++ is just cout to make it convenient to print...
Question is about Struct.
Oct 15 '08 #3

P: n/a
Neel wrote:
On Oct 15, 2:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Neel wrote:
>>any idea what's wrong???
You're posing C++ to a C group?
*Please* stop quoting signatures.
>
C++ is just cout to make it convenient to print...
Question is about Struct.
It's still impossible to answer without a prototype for GetUOID.

Why all those horrible casts?

Post something that compiles.

--
Ian Collins
Oct 15 '08 #4

P: n/a
On Oct 15, 2:05*pm, Neel <a.k.v...@gmail.comwrote:
Hi,
I made a simple struct and 'm trying to assign value to its members
but nothing is being actually assigned (I tested it by printing it...)

code :

struct header{
* * * * unsigned char msgtype;
* * * * unsigned char uoid[20];
* * * * unsigned char ttl;
* * * * unsigned char reserved;
* * * * unsigned int datalength;

};

struct header h;
h.msgtype=((unsigned char)0xFA);
cout<<"size "<< sizeof h<<endl; * * * * * * * *//prints "28"
unsigned char buf[20];
memset(h.uoid, 0, 20);
memcpy(h.uoid,(unsigned char *)GetUOID((char *)node_instance_id,
"hello", buf, sizeof buf),20); *//GetUOID is func
h.ttl=1; * * * * * * * * * * * * * * * * * * * * * *//prints nothing
cout<<"TTL "<<h.ttl<<endl;
h.reserved=0;
h.datalength=strlen(myhostname)+sizeof (myport);

any idea what's wrong???
You assigned a value of 1 to h.ttl, which is of type unsigned char.
What did you expect it to print out as?
If you had used printf instead of the "convenient" cout, you might
have
gotten a clue about what's wrong.
--
Fred Kleinschmidt
Oct 15 '08 #5

P: n/a
Neel wrote:
On Oct 15, 2:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
Neel wrote:
any idea what's wrong???
You're posing C++ to a C group?

--
Ian Collins

C++ is just cout to make it convenient to print...
Question is about Struct.
structs are part of C++ as well, with differences from C structs. Ask
in comp.lang.c++.

Brian
Oct 15 '08 #6

P: n/a
Neel <a.******@gmail.comwrites:
On Oct 15, 2:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
>Neel wrote:
any idea what's wrong???

You're posing C++ to a C group?

--
Ian Collins

C++ is just cout to make it convenient to print...
Question is about Struct.
At least one explanation of your problem is related to the use of <<
for output. Re-write as printf calls and I'll bet the problem goes
away (or at least becomes clear).

--
Ben.
Oct 15 '08 #7

P: n/a
On Oct 15, 3:53*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Neel <a.k.v...@gmail.comwrites:
On Oct 15, 2:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
Neel wrote:
any idea what's wrong???
You're posing C++ to a C group?
--
Ian Collins
C++ is just cout to make it convenient to print...
Question is about Struct.

At least one explanation of your problem is related to the use of <<
for output. *Re-write as printf calls and I'll bet the problem goes
away (or at least becomes clear).

--
Ben.
thank you
Oct 15 '08 #8

P: n/a
Neel wrote:
Hi,
I made a simple struct and 'm trying to assign value to its members
but nothing is being actually assigned (I tested it by printing it...)

code :
/* mha: code rewritten to be C. Try it and see what happens. */

#include <stdio.h>
#include <string.h>

struct header
{
unsigned char msgtype;
unsigned char uoid[20];
unsigned char ttl;
unsigned char reserved;
unsigned int datalength;
};

unsigned char *GetUOID(int i, char *s, unsigned char *buf,
size_t nchars)
{
char *t = (char *) buf;
snprintf(t, nchars, "%s %d", s, i);
return buf;
}
int main(void)
{
struct header h;
unsigned char buf[20];
int node_instance_id = 42;
char myhostname[] = "myhostname";
int myport;
h.msgtype = 0xFA;
printf("size %zu\n", sizeof h);
memset(h.uoid, 0, 20);
memcpy(h.uoid, GetUOID(node_instance_id, "hello", buf, sizeof buf),
20);
h.ttl = 1;
printf("TTL %u\n", h.ttl);
h.reserved = 0;
h.datalength = strlen(myhostname) + sizeof(myport);
return 0;
}
Oct 15 '08 #9

P: n/a
On Oct 15, 4:15*pm, Martin Ambuhl <mamb...@earthlink.netwrote:
Neel wrote:
Hi,
I made a simple struct and 'm trying to assign value to its members
but nothing is being actually assigned (I tested it by printing it...)
code :

/* mha: code rewritten to be C. *Try it and see what happens. */

#include <stdio.h>
#include <string.h>

struct header
{
* * *unsigned char msgtype;
* * *unsigned char uoid[20];
* * *unsigned char ttl;
* * *unsigned char reserved;
* * *unsigned int datalength;

};

unsigned char *GetUOID(int i, char *s, unsigned char *buf,
* * * * * * * * * * * * size_t nchars)
{
* * *char *t = (char *) buf;
* * *snprintf(t, nchars, "%s %d", s, i);
* * *return buf;

}

int main(void)
{
* * *struct header h;
* * *unsigned char buf[20];
* * *int node_instance_id = 42;
* * *char myhostname[] = "myhostname";
* * *int myport;
* * *h.msgtype = 0xFA;
* * *printf("size %zu\n", sizeof h);
* * *memset(h.uoid, 0, 20);
* * *memcpy(h.uoid, GetUOID(node_instance_id, "hello", buf, sizeof buf),
* * * * * * 20);
* * *h.ttl = 1;
* * *printf("TTL %u\n", h.ttl);
* * *h.reserved = 0;
* * *h.datalength = strlen(myhostname) + sizeof(myport);
* * *return 0;

}

Thank You
Oct 15 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.