extracting integer from array of char

M

Mantorok Redgormor

If I have an array of char and there is an integer located in that
char that was assigned by:
int foo=10;
*(int *)&array[position] = foo;

How can I extract that integer? Any portable way?
I can assign using the above method but can't retrieve it with the
above method.

Also, what is a portable method of sending and receiving a struct that
contains bitfields across a network to two different machines? one is
linux on x86 the other is solaris-sparc.. any idea? Very frustrating
problem.
 
C

CBFalconer

Mantorok said:
If I have an array of char and there is an integer located in
that char that was assigned by:
int foo=10;
*(int *)&array[position] = foo;

Bad. Casts are evil. Very likely to cause UB due to alignment
restrictions. Assuming array is an array of char, all you need
is:

array[position] = foo;

It's up to you to ensure that foo is in the range a char can
hold. You find out by testing against the values in limits.h.
How can I extract that integer? Any portable way?
I can assign using the above method but can't retrieve it with
the above method.

by:
foo = array[position];
Also, what is a portable method of sending and receiving a
struct that contains bitfields across a network to two different
machines? one is linux on x86 the other is solaris-sparc.. any
idea? Very frustrating problem.

Extract each field, convert it to a text representation, and send
that. The binary image on one machine need not have any
particular relationship to the binary image on another machine.

The recommended method even allows you to follow the process
during operation :)
 
M

Malcolm

Mantorok Redgormor said:
If I have an array of char and there is an integer located in that
char that was assigned by:
int foo=10;
*(int *)&array[position] = foo;

How can I extract that integer? Any portable way?
This is pretty horrible. If the same program does the assignment and
extraction, you can use memcpy()
memcpy(&foo, &array[position], sizeof(int)).
I can assign using the above method but can't retrieve it with the
above method.
That's surprising. If the compiler allows a cast one way it should allow it
in the reverse direction.
Also, what is a portable method of sending and receiving a struct that
contains bitfields across a network to two different machines? one is
linux on x86 the other is solaris-sparc.. any idea? Very frustrating
problem.
You are not guaranteed the same alignment for any structs (except within the
same program, of course).
You need to break the structure down into its elements, and send it one byte
at a time (all integers 32 bits big-endian, all chars ascii, all floats in
32-bit IEEE format, etc).
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top