|
(1, 2, 3} is 3 int's. It is not a pointer to an array of 3 int's.
The string/char * fudge dates from the early days of C before they realised they needed string as a first class data type.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Using 'compound literals[^]', you can:
#include <stdio.h>
struct testStruct_s {
int dummy;
int* array;
};
struct testStruct_s testStructArray[] = {
{ 0, (int[]){0, 1, 2, 3, -1}},
{ 0, (int[]){0, 1, 2, 3, 4, -1}}
};
int main()
{
for (int i=0; i<sizeof(testStructArray)/sizeof(testStructArray[0]); ++i)
{
printf("dummy = %d\n", testStructArray[i].dummy);
for (int k=0; testStructArray[i].array[k] != -1; ++k)
printf("array[%d]=%d\n", k, testStructArray[i].array[k]);
printf("\n");
}
}
|
|
|
|
|
Is this a compiler-specific feature or is it part of standard C? And is there a way that I can obtain the size of the arrays? The following simply returns the size of the pointer, not the array:
sizeof(testStructArray[0].array)
-- modified 17-Oct-17 8:20am.
|
|
|
|
|
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++.
So:
C99 compilers support it.- Your compiler suopports it even in
C89 mode.
|
|
|
|
|
Unfortunately I have very little knowledge about compilers and I don't know what C89 and C99 are. What I'm worried about is that my code won't compile in the future if we move to a different compiler.
|
|
|
|
|
Quote: Unfortunately I have very little knowledge about compilers and I don't know what C89 and C99 are It's time to learn, then.
Quote: What I'm worried about is that my code won't compile in the future if we move to a different compiler If you are really worried about then don't use the feature.
You might also check in advance if the compiler you are planning to use in future supports such a feature (or, generally C99 features).
|
|
|
|
|
CPallini wrote: It's time to learn, then.
Keep in mind that the OP is doing embedded code and consequently what is actually available might be less than strict usage would otherwise suggest.
|
|
|
|
|
Then it is easy enough to make your code simpler:
struct testStruct_s {
int dummy;
int* array;
};
int array1[] = {0, 1, 2, 3};
int array2[] = {0, 1, 2, 3, 4};
struct testStruct_s testStructArray[] = {
{0, array1},
{0, array2}
};
|
|
|
|
|
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.
In vino veritas
modified 17-Oct-17 22:42pm.
|
|
|
|
|
arnold_w wrote: an embedded microcontroller (ARM)
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.
|
|
|
|
|
receive error from all browsers with inet_e_download_failure.
not seen this one before
kt
|
|
|
|
|
And what does that have to do with C/C++/MFC?
|
|
|
|
|
Hi,
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 for this.
Thanks in advance!
|
|
|
|
|
If Google does not find any then you may have to do the design yourself.
|
|
|
|
|
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.
|
|
|
|
|
Member 12785864 wrote: Have you got a code snippet of how to create a control at runtime? No: as I said, if Google cannot find one then you most likely have to figure it out yourself.
|
|
|
|
|
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.
In vino veritas
|
|
|
|
|
Hello Leon,
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.
Richard
|
|
|
|
|
You don't have to add the resource you make a memory template, its the exact opposite of a resource ... I am not sure you are really getting it
In code you do this creating a memory block which you make the template in
bool CreateRotation (HWND parent){
int nchar, ret;
HGLOBAL hgbl;
LPDLGTEMPLATE lpdt;
LPWORD lpw;
LPWSTR lpwsz;
hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
if (!hgbl) return false;
lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
lpdt->style = WS_POPUP | WS_CAPTION | WS_SYSMENU;
lpdt->cdit = 0;
lpdt->x = 80;
lpdt->y = 80;
lpdt->cx = 300;
lpdt->cy = 100;
lpw = (LPWORD)(lpdt + 1);
*lpw++ = 0;
*lpw++ = 0;
lpwsz = (LPWSTR)lpw;
nchar = 1 + MultiByteToWideChar(CP_ACP, 0, LanguageString[652],
-1, lpwsz, 50);
lpw += nchar;
*lpw++ = 0;
GlobalUnlock(hgbl);
ret = (int)DialogBoxIndirectParam(GetModuleHandle(0),
(LPDLGTEMPLATE)hgbl,
parent,
(DLGPROC)RotationHandler,
0);
GlobalFree(hgbl);
if (ret == ID_Ok) {
PostMessage(parent, WM_COMMAND, WSC_UPDATEEVERYTHING, 0);
return true;
} else return false;
};
They are called runtime dialogs or dynamic dialogs but you can always know you are on the right track when you see the use of GlobalAlloc because you have to lock a block of memory to create the dialog in, which is why you don't need resource files etc.
Dynamic Dialog Boxes and C++ | Dr Dobb's[^]
In vino veritas
|
|
|
|
|
i tries but the rest lies here.
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i=0, largest, count =0;
int k, j;
printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do{
scanf("%d", &num[i]);
i++;
count++;
}while(num[i-1]!=0);
while(count>=0){
largest = 0;
for(j=0; j<=count-1; j++){
if(num[j] > largest){
largest = num[j];
j=k;
}
}
printf("%d", largest);
for(j=k; j<=count-1; j++){
num[j] = num[j+1];
}
count = count - 2;
}
return 0;
}
|
|
|
|
|
Maybe take a look at this[^] thread.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
So what's the problem? Can we assume you've stepped through the code, line by line, using the debugger (to see the value of each variable along the way)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
You need to stop writing code for a while and spend more time thinking about the problem. What steps are needed to accomplish your goal? Write them down on paper, line by line to understand what needs to be done at each step. I already showed you how to find the largest number in a sequence and you are still making it more complicated. As you read in the integers in the first loop you can check for the largest as you store them, you do not need a second loop. You also need to look at the statement in the second loop:
largest = num[j];
j=k;
What is the value of k at this point?
And in the final loop you are just moving numbers, with no regard to whether they are in any order.
|
|
|
|