Yes, now it works, thanks
but isn't there a way to achieve that without specify the second size?
I remember (from school) that it was possible...
Yes, now it works, thanks
but isn't there a way to achieve that without specify the second size?
I remember (from school) that it was possible...
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
I don't think it's possible.
The second size it's required to calculate the displacement to the base pointer.
For example
Qt Code:
int matrix[][5] = {{1,2,3,4,5},{7,8,9,10,11}};To copy to clipboard, switch view to plain text mode
When you write
the compiler generate
Qt Code:
*(matrix + i * ROW_SIZE + j) --> *(matrix + 1 * 5 + 3) --> *(matrix + 8)To copy to clipboard, switch view to plain text mode
A camel can go 14 days without drink,
I can't!!!
Mmh, yeah, you are right.
In fact I remembered wrong.
It is right like you explained.
Thank you!
PS: I would give you a forum "Thanks", but it seems that I can't because of a site bug I guess...
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
How about
Qt Code:
const char CONNECTIONS_AMOUNT=2; const char *DB[CONNECTIONS_AMOUNT] = {"db1","db2"}; const char *USERNAME[CONNECTIONS_AMOUNT] = {"root1","root2"}; const char *PASSWORD[CONNECTIONS_AMOUNT] = {"admin","admin"}; const char *HOST[CONNECTIONS_AMOUNT] = {"localhost","localhost"};To copy to clipboard, switch view to plain text mode
HTH
Raccoon29 (22nd July 2008)
Hey yes, it works too
What is that?! I mean, I never saw this kind of code, why is it accepted from the compiler?
EDIT: well, actally now is the linker that complains for a very long serie of multiple declarations of the constant I declared in such a way... but are declarations from files like qglobal.h so maybe it's fighting versus qt library...actually I don't get what's going on here...![]()
Last edited by Raccoon29; 22nd July 2008 at 11:43.
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
you must not put such stuff in headers;
header:
Qt Code:
enum { CONNECTIONS_AMOUNT=2 }; extern const char *DB[CONNECTIONS_AMOUNT]; extern const char *USERNAME[CONNECTIONS_AMOUNT];To copy to clipboard, switch view to plain text mode
.c-file
Qt Code:
const char *DB[CONNECTIONS_AMOUNT] = {"db1","db2"}; const char *USERNAME[CONNECTIONS_AMOUNT] = {"root1","root2"}; const char *PASSWORD[CONNECTIONS_AMOUNT] = {"admin","admin"};To copy to clipboard, switch view to plain text mode
otherwise you get one instance per file where the .h file is included - unless the linker can sort out the duplicates.
To your question:
you defined a big "rectangle" of x*y bytes.
I define a vector of pointers to strings.
This is not the same (in memory), but if you actually want an array of strings, perhaps closer to your intentions.
Also, note that
allocates CONNECTIONS_AMOUNT * MAX_STRING_SIZE bytes.Qt Code:
const char PASSWORD[CONNECTIONS_AMOUNT][MAX_STRING_SIZE]To copy to clipboard, switch view to plain text mode
My code "allocates" (needs) CONNECTIONS_AMOUNT * sizeof(pointer) + the actually needed string sizes.
Finally, your code could allow the strings to be modified (without const), mine cannot, as "somestring" is of type const char*.
HTH
Raccoon29 (22nd July 2008)
Oh, I see... yes, actually your code is the translation of what I intended to dothank you so much!
Another question if possible:
the included header has preprocessor filter:otherwise you get one instance per file where the .h file is included - unless the linker can sort out the duplicates.
haven't they any sort of effect here?Qt Code:
#ifndef DB_DEFINES #define DB_DEFINES // ...consts definitions... #endifTo copy to clipboard, switch view to plain text mode
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
yes, those guards do help against multiple declarations.
they do not help against multiple definitions (same source tranlated multiple times into different .o files).
Check if you have problems compiling or linking. It should be linking.
Move the definitions into a .c file and use extern as I suggested. Should help.
Raccoon29 (23rd July 2008)
In fact it is a linker problem and what you said is all right
I think to need to well study the difference between declaration and definition.
Thank you caduel for your explanations, from now I'll have to proceed alone![]()
--
raccoon29
"La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute"
Bookmarks