473,499 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct

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
9 2475
Neel wrote:
>
any idea what's wrong???
You're posing C++ to a C group?

--
Ian Collins
Oct 15 '08 #2
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
17556
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
3275
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
2582
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
3809
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
4307
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.