Subj : Re: how to initialize this? To : borland.public.cpp.borlandcpp From : Eric Date : Wed Oct 15 2003 08:02 pm Thomas Maeder [TeamB] wrote: > Eric writes: > > Your description is far too complicated for me to understand it. So just > a side note, which is unrelated to your question: > >> typedef struct { >> char *Name; >> int Value; >> }TAnimal; >> >> TAnimal Farm0[] = {{"dog", 0}, {"horse", 4}, {"cow", 6}}; > > This initializes the Name members of the Farm0 elements with the addresses > string literals. An implicit conversion from 'array of N char const' to > 'pointer to char' (non-const) is performed each time. Such a conversion is > conforming to the ISO C++ Standard, but that standard also says that it is > deprecated. > > The reason is that it's very easy for the inadvertent programmer to > accidentally modify a string literal through such a pointer to non-const. > Such a modification would cause your program to have undefined behavior. > > So better define your struct like this: > > typedef struct > { > char const *Name; > int Value; > } TAnimal; > > If this type is only used from C++ code (and not from C code), the usual > way to define it would be > > struct TAnimal > { > char const *Name; > int Value; > }; I was afraid of that (that i might not be very clear). What i am basically trying to do is to have an array of Farm structures each containing the same Animals. I want to intialize the animal names at compile time. At run time then i can read each farm from a section of a file and for each animal I will update the Value field. all the allowed animals are known at compile time so the file must only have those animals under each farm. If i can declare an initialized array of farms only once, then if i have to later add an animal i only have to do it once. Right now i am doing it this way and i hate it. TAnimal Farm0[] = {{"dog", 0}, {"horse", 4}, {"cow", 6}}; TAnimal Farm1[] = {{"dog", 0}, {"horse", 4}, {"cow", 6}}; TAnimal Farm2[] = {{"dog", 0}, {"horse", 4}, {"cow", 6}}; etc etc so if i add an animal i have to modify each declaration whereas if i could do it in one array like this TAnimal Farm[8][] = {{"dog", 0}, {"horse", 4}, {"cow", 6}}; I would only have to moidify it in one place to add a new animal Hopefully I have explained it better this time Thanks Eric .