473,765 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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," ABCDEFGHIJKLMNO PQRSTUVWXYZ");

memcpy(pHeader, &buffer,size of (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!
Nov 14 '05 #1
3 2995
In article <dd************ **************@ posting.google. com>,
Leo Nunez <le*******@gmai l.com> 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, "ABCDEFGHIJKLMN OPQRSTUVWXYZ");
:memcpy(pHeader ,&buffer,size of (struct tHeader));

What about internal padding and alignments?

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

:pHeader->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.
Nov 14 '05 #2


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," ABCDEFGHIJKLMNO PQRSTUVWXYZ");
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,size of (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!"

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

Nov 14 '05 #3


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," ABCDEFGHIJKLMNO PQRSTUVWXYZ");

memcpy(pHeader, &buffer,size of (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);

Nov 14 '05 #4

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

Similar topics

2
2245
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
27
2300
by: Shagy | last post by:
Greetings, I've been trying to find an equivant c funtion to the c++ copy function. Description: copy(char *cstring, size_t count, size_t offset); Copies "count" characters from a C-style string starting at offset.
1
3570
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
2
1366
by: Eric Sabine | last post by:
During a process in an app, I have a string created in code that I want automatically saved in the user's cache so he can move to another program and do a CTRL-V (or paste). This is easy I'm sure, but it's eluding me. Eric
9
7135
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
9
3785
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in code. for example, this is bad practice: header.h #include <string> using std::string;
0
1825
by: tommak | last post by:
It's a dream of human beings to build machines that can think and behave like human beings. The most important part of of such a machine is an artificial mind that can emulate the cognitive processing of human mind. This book, "Next Generation Artificial Intelligence, Artificial Mind - Part One - Basic Architecture and Cognitive Structure" introduces a basic artificial mind architecture and computational model for cognitive...
1
1616
by: Özden Irmak | last post by:
Hello, Does anybody know a way to get the local equvalents of strings used by explorer such as "Cut","Copy","Paste" ? Thanks in advance... Regards, Özden Irmak
3
1976
by: Jess | last post by:
Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string s("abcdefg"); vector<charv; vector<char>::iterator k = copy(s.begin(),s.end(),back_inserter(v));
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.