Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Copy String structure "A" to string structure "B"
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Eric Sosman, post: 2403861"] 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. .... and here's where my confusion begins, because the illustration doesn't resemble the problem statement. Fine; here's the structure type A and B will have, the `struct foo' mentioned above. 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. 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? 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. Filling the useless array with some data that doesn't appear relevant. Harmless, but it's still not clear why the array is here. 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. Since the fields are not valid C strings, all four of these printf() calls produce undefined behavior. Beyond my understanding, I'm afraid. What you want? 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? 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. Very well: "Hurrah!" [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Copy String structure "A" to string structure "B"
Top