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

  1. #1
    Registered User
    Join Date
    Oct 2007

    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.

    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.

    char foobar[3][9] {
    is there a quicker way to do this?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
        char foobar[3][9] = {   {"six"},
    Or at runtime,
        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
    Good solution...

  4. #4
    Registered User
    Join Date
    Oct 2007
    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
    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.
    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.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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