struct

N

Neel

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???
 
I

Ian Collins

*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.
 
F

Fred

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

Default User

Neel said:
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
 
B

Ben Bacarisse

Neel said:
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).
 
N

Neel

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

thank you
 
M

Martin Ambuhl

Neel said:
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;
}
 
N

Neel

Neel said:
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...)

/* 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
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top