In your first example, the struct member is a char *, that is, a pointer. Its size is fixed, although the string it points to (which is stored elsewhere) may be of variable size.
A debugger will show you what lives where at run time.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
I report you the first sentence of the linked page:
ISO C99 supports compound literals. A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer; it is an lvalue. As an extension, GCC supports compound literals in C89 mode and in C++.
I am basically writing a table (several hundred lines) with lots of information into source code (basically, my source code is the documentation for it) and it makes it so much easier to read if information is where it belongs and not somewhere else. That's why I don't want to use this approach.
For the record on GCC you can control the C standard it uses via the flag -std=C99 or C++ standard -std=C++98 (yes C99 equivalent in C++ is 98)
Your GCC compiler should be defaulted to at least C11 or C14, in C or C++ unless it's really old like GCC version 4.7.
Because of that I am going to presume that maybe you can't rely on the newest features (other suggestions).
Didn't see anyone mention the old school approach which should work on any C compiler unless perhaps one goes back to the 70s (perhaps even then.)
I am not going to try to even create pseudo code for this (way too long for me) but basic outline
- Add attributes for the size of each array
- All attributes EXCEPT arrays must be before arrays themselves. So arrays are at the end.
- Alloc the struct based on the size of the array PLUS the appropriate sizes of the arrays.
- Create helper methods that use and OFFSET to access the array pointer based on the sizes (attribute above) for each array.
Google for the following for examples. Look for examples that have something like "int array" at the end of the structure.
C programming language struct dynamic array
One gotcha which is probably still relevant is that, maybe, C compilers attempt to 'align' attributes in the struct, these days. That means it might add filler that you are unaware of. If so there should be a compiler option, to remove that.
This solution means that you cannot treat the array attributes as pointers. Specifically do not try to set them to another pointer. That is because the array storage is in the struct allocated block itself. So you cannot free it. And assignment would mean you couldn't free the assigned block either.
It doesn't matter to me where the arrays are actually stored, I just want the data the array is initialized with to be placed where it belong inside structure array. Basically, my source code is kind of a look-up table in this case, so it's purely a visual thing for readability.
i want to create a composite activex control to put together several contols. The control must be fully transparent and consist of several self written and 3rd party controls. I was searching the web for several days now and cannot find any sample / tutorial forthis.
Thanks in advance!
Yes, right, that's the question!
How can i load / create the additional controls, if not at designtime, maybe at runtime, without getting a linker error? Positioning the additional control will be done at runtime, when the contol changes size, anyway. I have included the LIB-file of my control into the references, but the error stays. Have you got a code snippet of how to create a control at runtime? I am really desperate for this.
You can do it via the resource system, you can extend it to even deal with custom controls and even activeX ones.
Search "Win32 dialog template at runtime" it is fairly commonly done with dialog templates but you can do it with windows or anything else.
Essentially the WM_CREATE (OnCreate in MFC) loads the dialog template from a resource and you create the setup on the fly.
I have included the Controls .h file and created a member variable in the new control's class, but when i try to show the <control>.ShowWindow(SW_SHOW) or MoveWindow function in the OnCreate or OnDraw nothing shows up.
I could not add the contol to the ressource editor, because I don't want to / need a dialog in the new control. Have you got any other idea?
Thanks a lot.
Last Visit: 31-Dec-99 19:00 Last Update: 30-Jan-23 18:56