[opensuse-programming] expanding const struct
Hi, sometimes I'm really impressed how bad my basic programming skills are :-(. I mean basically I know _a_ solution to the issue but I would like to get a pointer to the "best" solution. You'll find my simple example attached. Please note that I'm aware that this doesn't need to be nor is actually "real" C++ code. Take the struct and the const initialization of kProperties as given and not easily changeable. I just want to create a new array of PropertyPair which holds one more pair and use that later on. So one thing would be to allocate memory as needed by the const part and the additional part and copy both into that memory but I'm wondering if there is an easier/better way to achieve that. The resulting PropertyPair array could be const again if needed. Any hints for me? Thanks. Wolfgang
On Wed, 2008-03-19 at 15:06 +0100, Wolfgang Rosenauer wrote: Hi Wolfgang I have assumed that you want maximum C compatibility. In the code you present kProperties[] is effectively just a PropertyPair* to static data. If you want to be able to grow the list of properties then I would create a nul-terminated PropertyPair** with pointers to the static PropertyPair structures. This avoids duplicating and copying the PropertyPair structures into a PropertyPair array. I have attached my example. Using the nul-terminator also makes it easier to keep track of the array size. This solution will not work if you need to pass the property list as a PropertyPair* object to other functions. Although if the other functions are your own it might not be too difficult to refactor them.
participants (2)
-
Justin Finnerty
-
Wolfgang Rosenauer