473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

store contents of structure into an array

hi,
see if i want to copy the contents of a char arrary into a equally
size struct, assuming that the structure has no holes, padding etc., i
can use

structure = *(t_structure *) array;

but if i want to do things the other way round i.e. copy contents of
the structure to an char array. do i specifically need to have a loop
to do so, or is there a more elegant way to do it.

in case i am not clear have a look at the code below.

tia,
ash

int main()
{
typedef struct
{
char name[16];
char address[48];
char good;
char bad;
char ugly;
char bug;
} t_source;

char dest[68];

char *pt_char;

int i;

t_source source;

source = *(t_source *) dest;

pt_char = (char *) &source;

source.good='g';

for (i=0;i<=67;i++)
{
dest[i]=*pt_char;
pt_char++;
}
printf ("source.bad=%c char[64]=%c", source.bad, dest[64]);

}
Nov 14 '05 #1
6 3176
n00dle wrote:
hi,
see if i want to copy the contents of a char arrary into a equally
size struct, assuming that the structure has no holes, padding etc., i
can use

structure = *(t_structure *) array;

but if i want to do things the other way round i.e. copy contents of
the structure to an char array. do i specifically need to have a loop
to do so, or is there a more elegant way to do it.

in case i am not clear have a look at the code below.


memcpy if the memory doesn't overlap, memmove if it might.

-David
Nov 14 '05 #2


n00dle wrote:
hi,
see if i want to copy the contents of a char arrary into a equally
size struct, assuming that the structure has no holes, padding etc., i
can use

structure = *(t_structure *) array;
There are (at least) two things wrong with this. First,
the assumption of "no padding" is risky in the extreme, and
is an invitation to trouble later on. Second, you've got
potential alignment problems: if the alignment requirement
for the struct is stricter than that provided by the char
array, there's no telling what may happen.
but if i want to do things the other way round i.e. copy contents of
the structure to an char array. do i specifically need to have a loop
to do so, or is there a more elegant way to do it.


Following your first (dangerous) model, you could write

*(t_structure *)array = structure;

.... with all the same drawbacks as the original.

memcpy() moves arbitrarily-aligned chunks of memory
around -- but even so, the "no padding" assumption is a
possible problem. Why do you need to assume it? What
are you trying to do?

--
Er*********@sun.com

Nov 14 '05 #3
hi eric,
thanx for the suggestion, i was exactly looking for sth like that
only, though i never thought that i could do casting on the lhs also. i
do understand the issues raised by you. but i suppose if portability is
not a big issue, its quite okay to make such assumptions. so, if u take
a little care and define your structure such that you know that you
will not run into alignment problems then it okay, right?
as for what i am trying to do. well, primarily i was just bogged
down by this issue, for i knew that there ought to be a oneline
solution to getting this done, but i was not able to think of it
myself.
i am reading a SD card through a microprocessor. so, i basically
read data in chunks of 512 bytes into a buffer and dump them to a
structure so that i can easily acess the various fields in the data
which i was already able to do. but then i needed to make some changes
and put it back into the buffer, thats where i got stuck.
thanx again for the help, i was just not happy with the memcpy
solution, even though it was a oneliner.
regards,
ash

Nov 14 '05 #4


n0*****@yahoo.com wrote:
hi eric,
thanx for the suggestion, i was exactly looking for sth like that
only, though i never thought that i could do casting on the lhs also. i
do understand the issues raised by you. but i suppose if portability is
not a big issue, its quite okay to make such assumptions. so, if u take
a little care and define your structure such that you know that you
will not run into alignment problems then it okay, right?
as for what i am trying to do. well, primarily i was just bogged
down by this issue, for i knew that there ought to be a oneline
solution to getting this done, but i was not able to think of it
myself.
i am reading a SD card through a microprocessor. so, i basically
read data in chunks of 512 bytes into a buffer and dump them to a
structure so that i can easily acess the various fields in the data
which i was already able to do. but then i needed to make some changes
and put it back into the buffer, thats where i got stuck.
thanx again for the help, i was just not happy with the memcpy
solution, even though it was a oneliner.
regards,
ash


<off-topic>

If you show the same regard for the rules of your
programming languages that you do for those of English,
I predict a short and inglorious career. It's about
communication, ashnoodle: Do you want to be understood,
or do you just want to arrange letters in artistically
interesting combinations? e e cummings you're not.

</off-topic>

If you Really Truly Believe that you'll have no
trouble with struct padding or with endianness issues,
if you think all can be made well by clicking your heels
and reciting "There's no place like home," if you start
each day by clapping your hands to keep Tinkerbell going,
then just dispense with the array of characters altogether:
do your I/O directly to and from an instance of the struct.
That way, at least, you can stop worrying about alignment.

--
Er*********@sun.com

Nov 14 '05 #5
Hi Eric,
Hey, did I get you heated up 'cos I said, "I dont care!".
Well, I didn't mean any harm, you see. Still I shall say sorry
just in case i did I hurt your feelings or anything like that.

Please, please don't misunderstand me as I really, really
appreciate your help as I was really puzzled about that thing
almost since a day. I just hate to be misunderstood, although
I know its really easy to misunderstand me. As you have
yourself pointed that out and I myself had mentioned in the
first post itself, "in case i am not clear ...".

<floyd>
I can't explain, you would not understand.
This is not how I am.
</floyd>
<off-topic>

If you show the same regard for the rules of your
programming languages that you do for those of English,
I predict a short and inglorious career. It's about
communication, ashnoodle: Do you want to be understood,
or do you just want to arrange letters in artistically
interesting combinations? e e cummings you're not.
</off-topic>
Well, disclaimers aside, lets analyze a few of the things
that you had mentioned. I am not pretty sure about my
language rules, whether they be programming or literary.
Yeah, but you are correct on the basic issue, I do have a little
less regard for rules in general. As far as your predictions with
regard to my carrer are concerned, should I not say that you
are being a bit overbold and making certain far-fetched
assumptions. You, have not only terminated it pre-maturely but,
have defamed it also. I haven't even got a carrer yet and if you
are as good at making predictions as I certainly feel that you are
at programming, then I better be gravely concerned. Well, I just
do coding as a hobby and for pleasure, I am mostly more
interested in figuring out how and why things work rather than
accomplising anything seriously serious.
If you Really Truly Believe that you'll have no
trouble with struct padding or with endianness issues,
Hey, buddy I turst you for that that you shall be careful enough
and not land me up in trouble whatsoever. Well, I am a scientist,
or a to be one if you preffer that. As you know in science, there are
times when an its not possible to arrive at an exact solution to a
problem or even if its possible its too tedious and then then you
make certain assumptions to ease your burden. Even though you
know that they can lead to errors in the extreme conditions, but that
is a risk that you are willing to take in trade-off for the reduced
effort.
I find that thing pretty much justifiable. Well, getting a bit
technical
here for a moment, I feel that you would consider casting one
structure to another as equally dangerous thing to do. But, then one
routinely does that sort of thing while doing network programming.
So, is it that thoes structures are defined in manner that they can
never be misaligned. Secondly, let me rephrase my original query,
what would be the most efficient way (in terms of cpu clock cycles)
to copy contents of an structure (all members are of type char) into
an char array (of the same size), (a) you are willing to make
the above mentioned assumption and (b) you are not willing to make
any assumptions, however the total size of the individual members
of the structure is equal to the size of the array. Now, does one do
if he/she is severly limited by the processing power. Comming back
to the issue of rules, i feel that what you have mentioned is more of
a directive. It is perfectly legal to me the way I see things 'cos the
compiler doesn't even throw up a warning when you use that. BTW,
I am not atall trying to disregard the point that you have mentioned.
I love C for the fact that it gives you the power to do anything if you

understand what you are doing and otherwise it does anything if u
don't understand what you are doing and there by gives you an
opportunity to figure out what went wrong where and learn a hell lot
of things in the process. (In case you feel like saying that assembly
would be hell lot more powerful than C then just hang on for a while,
I basically don't disagree on that point.)
if you think all can be made well by clicking your heels
I just can't dance, I probably just wasn't build with the genes for
dancing included. I can probably attempt to sing but even then it
would be pretty lame for sure.
and reciting "There's no place like home," if you start
Yeah, you are actually quite right about that there is indeed no
place like home its just an artificial concept floated to keep people
perpetually delusioned.
each day by clapping your hands to keep Tinkerbell going, then just dispense with the array of characters altogether:
do your I/O directly to and from an instance of the struct.
That way, at least, you can stop worrying about alignment.


That indeed is a really fabulous suggestion. I do admire
obfuscated c codes, even though I might not be able to
understand them fully.

<nonesense>
Hey, since I have already talked to you for so long let me share
some abstract philosophy with you. Well, it is mainly inspired from
the themes of movies of the class of Matrix (first one, not the reloded

and unloaded crap), though I always find it very amusing as to why
Hollywood has such a pessimistic view of the future. Anyways,
considering the facts, increase in processing power, advancement
in AI algorithms, etc. it is quite natural that machine intelligence
would
develop tremendously in the years to come and then one day you may
end up with machines that comunicate with humans directly and that
would make them extremely powerful creatures. Now all this coupled
with a few half-minded people like me who cast arrays into structures
and vice-versa might just result in a catastrophe, like the kind
perdited
in movies and a war may ensue between the humans and the
machines and in such a situation humans shall be severly limited by
there inability to effectively and quickly understand machine talk
while
machines would be capable to understand humans easily. Hence, I
propose that we should start to teach assembly to children right from
the primary school and by the time time they finish off their high
school they should be able to load a jpeg image into a text editor
and be able to admire its beauty.
</nonsense>

Bye,
Ash

P.S.: Do you find my arrangement of letter combinations
artistically interesting?
All of the above may be taken at a light note, I sometimes
just say whatever comes to my mind.
Read, "Catcher in the Rye" by J.D. Salinger, if you havent
already done so.

Nov 14 '05 #6
then just dispense with the array of characters altogether:
do your I/O directly to and from an instance of the struct.
That way, at least, you can stop worrying about alignment.


Eric, thanks for the really fabulous suggestion. well what i
did is instead of doing away with the array, i did away
with the structure and instead define a pointer to the the struct
and pointed it to the array. that way i am be able to reference
the same memory location both as an array and as a structure
relatively easily.

when you are working with few KB of RAM and if u can save 512 bytes,
then its really fabulous. well actually 508, as i am still defining
a pointer to the struct. but thats okay, i can afford 4 bytes.

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using...
2
by: Mriganka | last post by:
I want to create a hierarchical tree from the array of strings obtained by extracting a jar file.I want to do this using Javascript. For ex, I am having the entries in the array as...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
5
by: moondaddy | last post by:
I'm caching a dataset in an asp.net session variable to hold a user's data. one data item I need to store is an image the user uploaded. My problem is that I don't know how to get the image into...
0
by: Toe007 | last post by:
Please help: I want to read out all the bytes of a structure and to write these to EEPROM i.e map the memory locations of the structure to EEPROM. Can one use a pointer and increment it to...
10
by: jotartub | last post by:
Hi. Does anyone know how can i crate an Array not managed, and it doesn't looses his contents. My problem is: I've a method that returns a managed array and i want to store it but i can't store it...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
2
by: overdrigzed | last post by:
Hello! I wanted to get the full contents of a character array stored in a struct, i.e. _fields_ = however, ctypes seems to try to return struct.array as a Python string rather than a...
5
rahulephp
by: rahulephp | last post by:
Hi there, can you please help me? i have data in array and i want to insert it in a single table. structure of table and array to be store is shown below: Please let me know how to store...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.