473,387 Members | 1,542 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,387 software developers and data experts.

Problem with structs and strings in C

Keep in mind my program is written in C ( not C++ ).

I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.
void AssignValues(info *source, info *destination)
{
*destination->name = *source->name;
*destination->class = *source->class;
*destination->alignment = *source->alignment;
}
Name, class and alignment are defined as
char variableName[20];
In a struct called info.

If my source is : test, TEST example
The destination is : t, T, e

Why is that ?

Thanks for your help.
Nov 14 '05 #1
7 1853
Bleakcabal <bl********@gdnmail.net> wrote:
Keep in mind my program is written in C ( not C++ ).

I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.


Use strcpy() or similar to copy strings.

Stig

--
brautaset.org
Nov 14 '05 #2
Bleakcabal wrote:

Keep in mind my program is written in C ( not C++ ).

I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.

void AssignValues(info *source, info *destination)
{
*destination->name = *source->name;
*destination->class = *source->class;
*destination->alignment = *source->alignment;
}

void AssignValues(info *source, info *destination)
{
*destination = *source;
}

--
pete
Nov 14 '05 #3
On 9 Feb 2004 05:12:27 -0800, bl********@gdnmail.net (Bleakcabal)
wrote:
Keep in mind my program is written in C ( not C++ ).

I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.
void AssignValues(info *source, info *destination)
{
*destination->name = *source->name;
*destination->class = *source->class;
*destination->alignment = *source->alignment;
}
Name, class and alignment are defined as
char variableName[20];
In a struct called info.

If my source is : test, TEST example
The destination is : t, T, e

Why is that ?

A few solutions have been posted, and they're correct given your
context so far [just be careful: if you use strcpy, make sure the
source strings have been initialized properly; if you use structure
assignment, keep in mind that it will cease to work correctly if you
were to change the type of any of your data members from arrays to
pointers...and that may very well end up being your next step ;-) ]

Another hint: if code involving arrays or pointers doesn't work right,
carefully check the /types/ of the expressions involved. In this case,
all six main expressions are of the form:
* array
Remember that array names used in /most/ expressions "decay" to being
a pointer to their first element, so type-wise that equates to:
* (char *)
The *'s cancel leaving "char" as the type. Hence the char-to-char
assignment.

Rule of thumb: There's no single C assignment expression that can
directly copy an array (you'd have to cheat and embed the array in a
structure and copy the structure, which is why Pete's solution works
so nicely in this case.)
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #4
Hi Pete,
this seems to be similar to a
question I posted earlier. But
won't
*destination = *source;


just copy the adresses of the array,
s.th like a non-deep copy? So both
structs share the strings?
Kind Regards,
Michael

Nov 14 '05 #5
On Mon, 09 Feb 2004 15:49:13 +0100, Michael <mi****@gmx.net> wrote:
Hi Pete,
this seems to be similar to a
question I posted earlier. But
won't
*destination = *source;


just copy the adresses of the array,
s.th like a non-deep copy? So both
structs share the strings?


Nope, there's no problem here because we're copying values, not
addresses. If the data members of the structure were pointers, /then/
we'd be copying addresses and inviting all sorts of undefined behavior
down the road when the program begins freeing them (or at least chaos
before that if the program expects the data being pointed to by the
two separate pointers to be distinct.)
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #6
Michael wrote:

this seems to be similar to a
question I posted earlier. But
won't
*destination = *source;


just copy the adresses of the array,
s.th like a non-deep copy? So both
structs share the strings?


You removed context quotations, including all definitions. IIR
you defined the fields within the structures as arrays, not as
pointers, so there is nothing deep to copy.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7


CBFalconer wrote:
Michael wrote:
this seems to be similar to a
question I posted earlier. But
won't

*destination = *source;


just copy the adresses of the array,
s.th like a non-deep copy? So both
structs share the strings?

You removed context quotations, including all definitions. IIR
you defined the fields within the structures as arrays, not as
pointers, so there is nothing deep to copy.


The op definitions are a little unclear. There is some evidence
that the members are typedef char array of 20 characters. If this
is the case, struct assignment will be safe as will using function
strcpy.

Example:

#include <stdio.h>
#include <string.h>

typedef char variableName[20];
typedef struct info
{
variableName name;
variableName class;
variableName alignment;
}info;

info init = {"No data","No data", "No data"};

void AssignValues1(const info *source, info *destination)
{
*destination = *source;
}

void AssignValues2(const info *source, info *destination)
{
strcpy(destination->name,source->name);
strcpy(destination->class, source->class);
strcpy(destination->alignment,source->alignment);
return;
}

void PrintInfo(const info *p)
{
char *s = "No Data";
printf("Name: %s\nClass: %s\nAlignment: %s\n\n",
p->name,p->class,p->alignment);
}

int main(void)
{
info myinfo = {"George Washington","Political Science","Junior"};
info newinfo = init;

puts("struct newinfo contents");
PrintInfo(&newinfo);

newinfo = myinfo;
puts("Using simple assignment");
PrintInfo(&newinfo);
newinfo = init;

puts("Using function AssignValues1");
AssignValues1(&myinfo, &newinfo);
PrintInfo(&newinfo);
newinfo = init;

puts("Using function AssignValues2");
AssignValues2(&myinfo, &newinfo);
PrintInfo(&newinfo);

return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8

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

Similar topics

8
by: Oliver Gerlich | last post by:
Hello, I want to transfer messages between a client and a server (over TCP sockets). A message consists of a message type (like a message "subject" :), the size of the attached data, and the data...
6
by: Simon Elliott | last post by:
I have ome new code which has to work with some legacy code which does a lot of memset's and memcmp's on structs of PODs. This leads me to want to do stuff like this: struct foo { unsigned...
7
by: Jeff Mott | last post by:
Ok, here's the situation. I've got a file whose first line indicates how many n number of names will follow. And some more data after that, but I havn't gotten that far yet. So what I've got is......
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
3
by: mearvk | last post by:
I am unable to get Xerces to write out attributes from a struct which I am able to print out and verify is correct. The structs are defined below as is subsection of the output. int...
24
by: Alexander Mahone | last post by:
Hello, I'm looking for an hash function to be used for an hash table that will contain structs of a certain kind. I've looked into Sourceforge.net, but so far I've found only hash functions for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...
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...

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.