Results 1 to 11 of 11

Thread: String initialization issue

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Post String initialization issue

    Hi,

    I'm trying to define some consts to use as interface for a class...
    The point is, I have to define const arrays of strings (that is a matrix), but after many years of C++ programming, this is actually the first time that I have such a task... so I need a refresh...

    The following code:
    Qt Code:
    1. const char CONNECTIONS_AMOUNT=2;
    2. const char DB[CONNECTIONS_AMOUNT-1][] = {"db1","db2"};
    3. const char USERNAME[CONNECTIONS_AMOUNT-1][] = {"root1","root2"};
    4. const char PASSWORD[CONNECTIONS_AMOUNT-1][] = {"admin","admin"};
    5. const char HOST[CONNECTIONS_AMOUNT-1][] = {"localhost","localhost"};
    To copy to clipboard, switch view to plain text mode 
    doesn't work and the compiler complains:
    multidimensional arrays must have bounds for all dimensions except the first
    so following a suggestion I found on the net:
    Qt Code:
    1. const char CONNECTIONS_AMOUNT=2;
    2. const char DB[][CONNECTIONS_AMOUNT] = {"db1","db2"};
    3. const char USERNAME[][CONNECTIONS_AMOUNT] = {"root1","root2"};
    4. const char PASSWORD[][CONNECTIONS_AMOUNT] = {"admin","admin"};
    5. const char HOST[][CONNECTIONS_AMOUNT] = {"localhost","localhost"};
    To copy to clipboard, switch view to plain text mode 
    and now the compiler complains:
    initializer-string for array of chars is too long
    (perhaps because so it expects arrays of 2 chars, that is CONNECTIONS_AMOUNT).

    So, what is the simple solution and the stupid mistake for this?
    Thank you for your time
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String initialization issue

    Try This

    Qt Code:
    1. const int MAX_STRING_SIZE = 100;
    2. const int CONNECTIONS_AMOUNT = 2;
    3. const char DB [CONNECTIONS_AMOUNT][MAX_STRING_SIZE] = {"db1","db2"};
    4. const char USERNAME[CONNECTIONS_AMOUNT][MAX_STRING_SIZE] = {"root1","root2"};
    5. const char PASSWORD[CONNECTIONS_AMOUNT][MAX_STRING_SIZE] = {"admin","admin"};
    6. const char HOST [CONNECTIONS_AMOUNT][MAX_STRING_SIZE] = {"localhost","localhost"};
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    Raccoon29 (22nd July 2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: String initialization issue

    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 "

  5. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String initialization issue

    Quote Originally Posted by Raccoon29 View Post
    I remember (from school) that it was possible...
    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:
    1. 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
    Qt Code:
    1. matrix[1][3]
    To copy to clipboard, switch view to plain text mode 
    the compiler generate
    Qt Code:
    1. *(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!!!

  6. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: String initialization issue

    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 "

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: String initialization issue

    How about
    Qt Code:
    1. const char CONNECTIONS_AMOUNT=2;
    2. const char *DB[CONNECTIONS_AMOUNT] = {"db1","db2"};
    3. const char *USERNAME[CONNECTIONS_AMOUNT] = {"root1","root2"};
    4. const char *PASSWORD[CONNECTIONS_AMOUNT] = {"admin","admin"};
    5. const char *HOST[CONNECTIONS_AMOUNT] = {"localhost","localhost"};
    To copy to clipboard, switch view to plain text mode 

    HTH

  8. The following user says thank you to caduel for this useful post:

    Raccoon29 (22nd July 2008)

  9. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: String initialization issue

    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 10:43.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  10. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: String initialization issue

    you must not put such stuff in headers;

    header:
    Qt Code:
    1. enum { CONNECTIONS_AMOUNT=2 };
    2. extern const char *DB[CONNECTIONS_AMOUNT];
    3. extern const char *USERNAME[CONNECTIONS_AMOUNT];
    To copy to clipboard, switch view to plain text mode 

    .c-file
    Qt Code:
    1. const char *DB[CONNECTIONS_AMOUNT] = {"db1","db2"};
    2. const char *USERNAME[CONNECTIONS_AMOUNT] = {"root1","root2"};
    3. 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
    Qt Code:
    1. const char PASSWORD[CONNECTIONS_AMOUNT][MAX_STRING_SIZE]
    To copy to clipboard, switch view to plain text mode 
    allocates CONNECTIONS_AMOUNT * MAX_STRING_SIZE bytes.
    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

  11. The following user says thank you to caduel for this useful post:

    Raccoon29 (22nd July 2008)

  12. #9
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: String initialization issue

    Oh, I see... yes, actually your code is the translation of what I intended to do thank you so much!

    Another question if possible:
    otherwise you get one instance per file where the .h file is included - unless the linker can sort out the duplicates.
    the included header has preprocessor filter:
    Qt Code:
    1. #ifndef DB_DEFINES
    2. #define DB_DEFINES
    3. // ...consts definitions...
    4. #endif
    To copy to clipboard, switch view to plain text mode 
    haven't they any sort of effect here?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  13. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: String initialization issue

    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.

  14. The following user says thank you to caduel for this useful post:

    Raccoon29 (23rd July 2008)

  15. #11
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: String initialization issue

    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 "

Similar Threads

  1. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.