Thread: Is there a quicker way to initialise an array?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    Is there a quicker way to initialise an array?

    heres my problem i am Initializing an array of 3 strings... i know how to initialize an array with one string.

    Code:
    char foobar[6] = "marry";
    however i have a 2d array, and i need to initialize 3 strings into it is the only way to so it like this.

    Code:
    char foobar[3][9] {
         {'s','i','x','\0'},
         {'t','w','e','l','v','e','\0'} 
         ........................
    };
    is there a quicker way to do this?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes,
    Code:
        char foobar[3][9] = {   {"six"},
                                {"twelve"},
                                {"seven"}
                            };
    Or at runtime,
    Code:
        strcpy(foobar[0], "six");
        strcpy(foobar[1], "twelve");
        strcpy(foobar[2], "seven");
    Last edited by zacs7; 10-24-2007 at 06:23 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Good solution...

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    yea thats awesome!! i tried something similar to your first suggestion but kept getting a run time error! your solution is prefect! thankyou very much! Zacs7!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just so you know, you can leave off the inner braces. Most compilers warn you about this, however, and I don't recommend doing it myself; but you sometimes see other people's code that does this, and if you don't know that you can do this it can be very confusing.
    Code:
    int num[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    I'm not sure what standard this is, if any, but gcc gives a warning with -ansi, so it's probably not C89. Don't use it, it's just a curiosity.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your braces should match the structure of the data you're trying to initialise, if you want to be really good about it. Some compilers / tools will complain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM