Results 1 to 6 of 6

Thread: loading array values directly

  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default initializing array values upon declaration

    I want to load 9 predetermined strings and 8 predetermined integers to two different arrays. I have seen code of the form:
    Qt Code:
    1. char *namelist[] = {"Ram", "Shyam", "Dhyaan",};
    2. int numlist[] = {0, 1, 4, 2, 5, 3, 6, 11,};
    To copy to clipboard, switch view to plain text mode 
    But I want to know some things:
    1. what is the * for in the first statement?
    2. since a string is already a character array and I need to create an array of strings, don't I need to write char *namelist[][] (two []-s)?
    3. is the comma necessary before the closing brace?
    4. is the semicolon necessary after the closing brace?
    Last edited by jamadagni; 7th January 2006 at 15:45. Reason: making title clearer
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: initializing array values upon declaration

    Quote Originally Posted by jamadagni
    what is the * for in the first statement?
    namelist is an array of strings and strings in C are of char * type.

    Quote Originally Posted by jamadagni
    don't I need to write char *namelist[][] (two []-s)?
    No, futhermore you can't declare a multidimentional array without specifying dimentions.

    Qt Code:
    1. int wont_work[][] = { {1,2,3}, {4,5,6} }; // error
    2. int will_work[][3] = { {1,2,3}, {4,5,6} };
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by jamadagni
    is the comma necessary before the closing brace?
    No.

    Quote Originally Posted by jamadagni
    is the semicolon necessary after the closing brace?
    Yes.

  3. #3
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: initializing array values upon declaration

    Quote Originally Posted by jacek
    strings in C are of char * type.
    Do I read that as "strings in C are of the nature of pointers to char-length memory spaces"?
    futhermore you can't declare a multidimentional array without specifying dimentions.
    Qt Code:
    1. int wont_work[][] = { {1,2,3}, {4,5,6} }; // error
    2. int will_work[][3] = { {1,2,3}, {4,5,6} };
    To copy to clipboard, switch view to plain text mode 
    How come you still don't need to specify the first dimension? And if I declare char *namelist[] and include lots of strings should it not be mandatory that I specify the second dimension - i.e. the maximum length of strings?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: initializing array values upon declaration

    Quote Originally Posted by jamadagni
    Do I read that as "strings in C are of the nature of pointers to char-length memory spaces"?
    Yes.

    Quote Originally Posted by jamadagni
    How come you still don't need to specify the first dimension?
    It's one of those inconsistencies and in fact arrays are evil.

    Quote Originally Posted by jamadagni
    And if I declare char *namelist[] and include lots of strings should it not be mandatory that I specify the second dimension - i.e. the maximum length of strings?
    Because you declare an array of pointers, not a two dimensional array.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: initializing array values upon declaration

    Quote Originally Posted by jacek
    It's one of those inconsistencies and in fact arrays are evil.
    Isn't that because you provide the dimension of the array right in the assignment? And furthermore, is that a standard or a compiler extension to allow such a declaration and assignment statement?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: initializing array values upon declaration

    Quote Originally Posted by wysota
    is that a standard or a compiler extension to allow such a declaration and assignment statement?
    K&R use that form of assignment in their book.

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.