union

D

dg

helo i need some help with unions

i have a structure that has two int's and one five byte char string

and i need to convert the hole thing into a string
to pass to another pc

but i cnat get it to work

code__


// main prog
#include <stdio.h>
#include <winsock.h>

struct PACKETFRAME


int num1;
int num2;
char junk[5];
};




union MYUNION
{
PACKETFRAME dataframe;
char string[21];
};
union MYUNION2
{

char string[21];
PACKETFRAME dataframe;
};
main(void)
{

PACKETFRAME sizor;
MYUNION thing1;
MYUNION2 thing2;
printf("size of sizor %d \n",sizeof sizor);
printf("size of thing1 %d \n",sizeof thing1);
printf("size of thing2 %d \n",sizeof thing2);
thing1.dataframe.num1 = 10;
thing1.dataframe.num2 = 15;
lstrcpy (thing1.dataframe.junk, "abcd");
printf(" %d %d %s
\n",thing1.dataframe.num1,thing1.dataframe.num2,thing1.dataframe.junk);
printf ("thing1 %s, thing2 %s \n" ,thing1.string,thing2.string);
lstrcpy(thing2.string,thing1.string);
printf ("thing1 %s, thing2 %s \n" ,thing1.string,thing2.string);
printf ("%d %d %s
\n",thing2.dataframe.num1,thing2.dataframe.num2,thing2.dataframe.junk);


}

code__end
 
T

those who know me have no need of my name

in comp.lang.c i read:
helo i need some help with unions

i have a structure that has two int's and one five byte char string

and i need to convert the hole thing into a string
to pass to another pc

but i cnat get it to work

that's because you aren't trying to convert anything, and you should be. a
union does not accomplish conversion, it merely allows two things to occupy
the same space. most often the printf family of functions is what you
want, e.g.,

/* the structure you discussed above */
struct foo { int one, two; char bytes[6]; };

/* i decided to define the transfer format to be a `line of comma
separated values', hence it ends with a \n */
#define XFERFMT "%d,%d,%s\n"

int main(void)
{
struct foo x;

/* code that assigns values to all the members of x here */

/* calculate the space needed for the string */
int len = snprintf(0, 0, XFERFMT, x.one, x.two, x.bytes);
if (0 > len) abort();

/* allocate that much space */
char *xfer = malloc(len+1);
if (0 == xfer) abort();

/* convert the values to string representations */
snprintf(xfer, len+1, XFERFMT, x.one, x.two, x.bytes);
/* the truly paranoid would check that snprintf returned len+1 */

/* transmit the string in xfer to your peer */

free(xfer);
}
 
M

marko djogatovic

I belive that this is solution for your problem

#pragma pack(1)
#include <stdio.h>
#include <string.h>

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */

pf1.num1 = 10;
pf1.num2 = 15;
strcpy (pf1.junk, "abcd");

memcpy(buff, (char*)&pf1, 13);
memcpy((char*) &pf2, buff, 13);

printf("%d %d %s", pf2.num1, pf2.num2, pf2.junk);

return 0;
}

Marko Djogatovic
 
R

Richard Heathfield

marko said:
I belive that this is solution for your problem

#pragma pack(1)

Non-portable pragma.
#include <stdio.h>
#include <string.h>

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */

Better:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */
pf1.num1 = 10;
pf1.num2 = 15;
strcpy (pf1.junk, "abcd");

With you so far.
memcpy(buff, (char*)&pf1, 13);

The cast is spurious.
memcpy((char*) &pf2, buff, 13);

So is this one.

<snip>
 
A

Artie Gold

Richard said:
marko djogatovic wrote:

I belive that this is solution for your problem

#pragma pack(1)


Non-portable pragma.

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

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */


Better:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */
With you so far.




The cast is spurious.




So is this one.

<snip>

--ag
 
C

Chris Mears

Artie Gold said:
Richard said:
marko said:
struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */
Better:
unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare
occasions where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-)
*/

Does C++ also use parentheses instead of square brackets for specifying
array size?
 
R

Richard Heathfield

Artie said:
Richard said:
marko djogatovic wrote:

I belive that this is solution for your problem

#pragma pack(1)


Non-portable pragma.

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

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */


Better:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */


No, just too used to typedef. I didn't notice that he hadn't used it.

BTW Chris bites you back in a parallel reply. :)
 
D

dg

Artie Gold said:
Richard said:
marko djogatovic wrote:

I belive that this is solution for your problem

#pragma pack(1)


Non-portable pragma.

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

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */


Better:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */

dont matter my complier is c++ i just dont like classes.
--ag

--
Artie Gold -- Austin, Texas



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
D

dg

allright fellers thanks for the help


if any are easily offended stop here
oh a little something yall can copy as a generic anti miss posters

just generalize your code when you need help here
if you need specific os or plat form help look for that newsgroup

that theory helped me
I would have been seriously flamed if I asked how to send a struct through
tcp/ip in windows
I just used my head and asked the right question
 
A

Artie Gold

Chris said:
Richard said:
marko djogatovic wrote:


struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */

Better:
unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare
occasions where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-)
*/


Does C++ also use parentheses instead of square brackets for specifying
array size?
GACK! Of course not. ;-(
[He who picketh nits, more nits doth he create. Or something.]

Humbly,
--ag
 
A

Artie Gold

dg said:
Artie Gold said:
Richard said:
marko djogatovic wrote:



I belive that this is solution for your problem

#pragma pack(1)


Non-portable pragma.



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

struct PACKETFRAME {
int num1;
int num2;
char junk[5];
};

int main() {
struct PACKETFRAME pf1, pf2;
char buff[13]; /* 4+4+5=13 */


Better:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */
Of course that should be:
unsigned char buff[sizeof(struct PACKETNAME)];
dont matter my complier is c++ i just dont like classes.
Actually it does. In C++ you wouldn't need the `struct' keyword, in C
(and this _is_ c.l.c) you do.

<snip>

--ag
 
Z

Zoran Cutura

dg said:
good now i need to know how to put it back in a struct.

The obvious counterpart to sprintf/snprintf is sscanf, but strtol with
strpbrk might fix your needs too.
 
Z

Zoran Cutura

dg said:
Artie Gold said:
Richard said:
marko djogatovic wrote:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare occasions
where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */

Obviously unsigned char buff[sizeof(struct PACKETNAME];
was meant.
dont matter my complier is c++ i just dont like classes.

So you mean you write C programs and want the C++-compiler to compile
them cleanly. That restricts your programs to the common subset of C and
C++ which one day is likly to bite your leg.
 
R

Richard Heathfield

Zoran said:
dg said:
Artie Gold said:
Richard Heathfield wrote:
marko djogatovic wrote:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare
occasions where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */

Obviously unsigned char buff[sizeof(struct PACKETNAME];
was meant.

Close. :)

Obviously, unsigned char buff[sizeof(struct PACKETNAME)]; was meant.
 
Z

Zoran Cutura

Richard Heathfield said:
Zoran said:
dg said:
Richard Heathfield wrote:
marko djogatovic wrote:

unsigned char buff[sizeof(PACKETFRAME)]; /* one of those rare
occasions where sizeof(type) is useful. */

unsigned char buff(sizeof(struct PACKETNAME)); /* too much C++? ;-) */

Obviously unsigned char buff[sizeof(struct PACKETNAME];
was meant.

Close. :)

Obviously, unsigned char buff[sizeof(struct PACKETNAME)]; was meant.

Naturally a correction needs to introduce another error! ;-)
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top