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??? 9 2462
Neel wrote:
>
any idea what's wrong???
You're posing C++ to a C group?
--
Ian Collins
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.
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
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
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
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.
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
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;
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;
}...
|
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
{
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |