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???
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 9:05:59 PM |
|
Neel wrote:
>
> any idea what's wrong???
You're posing C++ to a C group?
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/15/2008 9:12:22 PM
|
|
On Oct 15, 2:12=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> 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.
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 9:38:25 PM
|
|
Neel wrote:
> On Oct 15, 2:12 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> 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
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/15/2008 9:41:50 PM
|
|
On Oct 15, 2:05=A0pm, Neel <a.k.v...@gmail.com> 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 :
>
> struct header{
> =A0 =A0 =A0 =A0 unsigned char msgtype;
> =A0 =A0 =A0 =A0 unsigned char uoid[20];
> =A0 =A0 =A0 =A0 unsigned char ttl;
> =A0 =A0 =A0 =A0 unsigned char reserved;
> =A0 =A0 =A0 =A0 unsigned int datalength;
>
> };
>
> struct header h;
> h.msgtype=3D((unsigned char)0xFA);
> cout<<"size "<< sizeof h<<endl; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//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); =A0//GetUOID is func
> h.ttl=3D1; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0//prints nothing
> cout<<"TTL "<<h.ttl<<endl;
> h.reserved=3D0;
> h.datalength=3Dstrlen(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
|
|
0
|
|
|
|
Reply
|
fred.l.kleinschmidt (236)
|
10/15/2008 10:16:22 PM
|
|
Neel wrote:
> On Oct 15, 2:12�pm, Ian Collins <ian-n...@hotmail.com> wrote:
> > 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
|
|
0
|
|
|
|
Reply
|
defaultuserbr (3657)
|
10/15/2008 10:38:35 PM
|
|
Neel <a.k.vora@gmail.com> writes:
> On Oct 15, 2:12 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> 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.
|
|
0
|
|
|
|
Reply
|
ben.usenet (6515)
|
10/15/2008 10:53:41 PM
|
|
On Oct 15, 3:53=A0pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Neel <a.k.v...@gmail.com> writes:
> > On Oct 15, 2:12=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> >> 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. =A0Re-write as printf calls and I'll bet the problem goes
> away (or at least becomes clear).
>
> --
> Ben.
thank you
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 11:03:41 PM
|
|
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;
}
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
10/15/2008 11:15:08 PM
|
|
On Oct 15, 4:15=A0pm, Martin Ambuhl <mamb...@earthlink.net> wrote:
> 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. =A0Try it and see what happens. */
>
> #include <stdio.h>
> #include <string.h>
>
> struct header
> {
> =A0 =A0 =A0unsigned char msgtype;
> =A0 =A0 =A0unsigned char uoid[20];
> =A0 =A0 =A0unsigned char ttl;
> =A0 =A0 =A0unsigned char reserved;
> =A0 =A0 =A0unsigned int datalength;
>
> };
>
> unsigned char *GetUOID(int i, char *s, unsigned char *buf,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 size_t nchars)
> {
> =A0 =A0 =A0char *t =3D (char *) buf;
> =A0 =A0 =A0snprintf(t, nchars, "%s %d", s, i);
> =A0 =A0 =A0return buf;
>
> }
>
> int main(void)
> {
> =A0 =A0 =A0struct header h;
> =A0 =A0 =A0unsigned char buf[20];
> =A0 =A0 =A0int node_instance_id =3D 42;
> =A0 =A0 =A0char myhostname[] =3D "myhostname";
> =A0 =A0 =A0int myport;
> =A0 =A0 =A0h.msgtype =3D 0xFA;
> =A0 =A0 =A0printf("size %zu\n", sizeof h);
> =A0 =A0 =A0memset(h.uoid, 0, 20);
> =A0 =A0 =A0memcpy(h.uoid, GetUOID(node_instance_id, "hello", buf, sizeof =
buf),
> =A0 =A0 =A0 =A0 =A0 =A0 20);
> =A0 =A0 =A0h.ttl =3D 1;
> =A0 =A0 =A0printf("TTL %u\n", h.ttl);
> =A0 =A0 =A0h.reserved =3D 0;
> =A0 =A0 =A0h.datalength =3D strlen(myhostname) + sizeof(myport);
> =A0 =A0 =A0return 0;
>
> }
>
>
Thank You
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 11:26:11 PM
|
|
|
9 Replies
29 Views
(page loaded in 0.254 seconds)
|