Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Copy String structure "A" to string structure "B"

Reply
Thread Tools

Copy String structure "A" to string structure "B"

 
 
Leo Nunez
Guest
Posts: n/a
 
      02-08-2005
Hello!

I need copy from structure "A" to "B" that contains "strings" in a one line code.

Me problem like this :

typedef struct tHeader{
char field1[4];
char field2[3];
char field3[2];
char field4[1];
};

struct tHeader Header,*pHeader;

char buffer[100];

pHeader=&Header;

strcpy(buffer,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

memcpy(pHeader,&buffer,sizeof (struct tHeader));

printf("field1 : %s : ",pHeader->field1);
printf("field2 : %s : ",pHeader->field2);
printf("field3 : %s : ",pHeader->field3);
printf("field4 : %s : ",pHeader->field4);

What i need ?

pHeader->field1 ==>"ABCD"
pHeader->field2 ==>"ABC"
pHeader->field3 ==>"AB"
pHeader->field4 ==>"A"

Someone helpme ?

Cheers!
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-08-2005
In article <(E-Mail Removed) >,
Leo Nunez <(E-Mail Removed)> wrote:
:I need copy from structure "A" to "B" that contains "strings" in a one line code.

In the code you show, you are not copying structure to structure:
you are copying from a character array to a structure.

:typedef struct tHeader{
: char field1[4];
: char field2[3];
: char field3[2];
: char field4[1];
:};

:strcpy(buffer,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
:memcpy(pHeader,&buffer,sizeof (struct tHeader));

What about internal padding and alignments?

rintf("field1 : %s : ",pHeader->field1);

Header->field1 ==>"ABCD"

You don't null terminate your strings, so the output could
run on indefinitely.
--
100% of all human deaths occur within 100 miles of Earth.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-08-2005


Leo Nunez wrote:
> Hello!
>
> I need copy from structure "A" to "B" that contains "strings" in a one line code.


Assuming A and B are the same type of structure -- that
is, that they are both `struct foo' -- this is simple: B = A
will do it.

> Me problem like this :


.... and here's where my confusion begins, because the
illustration doesn't resemble the problem statement.

> typedef struct tHeader{
> char field1[4];
> char field2[3];
> char field3[2];
> char field4[1];
> };


Fine; here's the structure type A and B will have, the
`struct foo' mentioned above.

> struct tHeader Header,*pHeader;


All right, we have an instance of the structure, and
it's called Header. We don't know yet whether this is the
A or the B, but it'll be one or the other. We also have
a pointer that can refer to structures of this type, which
isn't something that was present in the problem statement --
but maybe things will become clearer as I read further.

> char buffer[100];


An array of characters? What has that to do with the
price of eggs? You were asking about two structures; what
is this array for?

> pHeader=&Header;


All right, the pointer now refers to the structure.
We still don't know whether it's the A or the B structure
(or why we're bothering with a pointer at all), but it
makes sense.

> strcpy(buffer,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");


Filling the useless array with some data that doesn't
appear relevant. Harmless, but it's still not clear why
the array is here.

> memcpy(pHeader,&buffer,sizeof (struct tHeader));


Probably valid, but dubious in the extreme. It overlays
the structure instance with as many characters as will fit.
The first four characters go into the field1 element, and
some of the subsequent characters (you don't know exactly
which) go into the other elements. It is possible, although
unlikely, that sizeof(struct tHeader) is greater than the
size of buffer, in which case memcpy() will try to read beyond
the bounds of the array with unpredictable consequences.

Note that the elements of the structure will not be valid
C strings, since they lack '\0' terminators. Note also that
`&buffer' should probably be just `buffer'.

I do not understand what you are trying to accomplish
with this very strange operation.

> printf("field1 : %s : ",pHeader->field1);
> printf("field2 : %s : ",pHeader->field2);
> printf("field3 : %s : ",pHeader->field3);
> printf("field4 : %s : ",pHeader->field4);


Since the fields are not valid C strings, all four of
these printf() calls produce undefined behavior.

> What i need ?


Beyond my understanding, I'm afraid. What you want?

> pHeader->field1 ==>"ABCD"
> pHeader->field2 ==>"ABC"
> pHeader->field3 ==>"AB"
> pHeader->field4 ==>"A"


Is this the output you're trying to produce? I don't
understand the connection between this output and the data
that are actually deposited into the structure elements.
We know that field1 will contain the four characters A B C D
(and no terminating '\0' because there's no room for it), but
we don't know what the remaining fields will contain. We know
they will *not* contain any of A B C D, though, since those
characters landed in field1 and don't occur again in buffer.
Why do you expect one A to appear four times?

> Someone helpme ?


You'll need to explain your problem more clearly. What
you asked was simple enough, but your illustration makes no
sense at all. Not to me, anyhow.

> Cheers!


Very well: "Hurrah!"

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Neil Kurzman
Guest
Posts: n/a
 
      02-09-2005


Leo Nunez wrote:

> Hello!
>
> I need copy from structure "A" to "B" that contains "strings" in a one line code.
>
> Me problem like this :
>
> typedef struct tHeader{
> char field1[4];
> char field2[3];
> char field3[2];
> char field4[1];
> };
>
> struct tHeader Header,*pHeader;
>
> char buffer[100];
>
> pHeader=&Header;
>
> strcpy(buffer,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
>
> memcpy(pHeader,&buffer,sizeof (struct tHeader));
>
> printf("field1 : %s : ",pHeader->field1);
> printf("field2 : %s : ",pHeader->field2);
> printf("field3 : %s : ",pHeader->field3);
> printf("field4 : %s : ",pHeader->field4);
>
> What i need ?
>
> pHeader->field1 ==>"ABCD"
> pHeader->field2 ==>"ABC"
> pHeader->field3 ==>"AB"
> pHeader->field4 ==>"A"
>
> Someone helpme ?
>
> Cheers!


I would think you would need a union to do that not a struct.
your printfs will not work since you strings have no nulls.

try printf("field1 : %4s : ",pHeader->field1);



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does std::string do a shallow copy in copy construtor and assign operator ? suman.nandan@gmail.com C++ 2 05-26-2007 07:47 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
using File.Copy to copy files to shared hosting site Steve Richter ASP .Net 4 04-18-2005 03:06 PM
VB.Net Copy Project fails because copy directory is marked executable. John Blair ASP .Net 4 01-12-2005 02:24 AM



Advertisments