Multi-dimensional array initialization

Discussion in 'C Programming' started by masood.iqbal@lycos.com, Jan 23, 2005.

  1. Guest

    Hi,

    I have seen at least two ways to initialize multi-dimensional arrays in
    C. One of the ways is shown in a sample code snippet below. The other
    way does not make use of any intermediate braces. In other words, all
    the entries are listed under the same pair of enclosing braces. For
    example:

    char* mdTbl[3][5] = { "One", "Two", "Three", "Four","Five", "Six",
    "Seven",
    "Eight", "Nine", "Ten", "Eleven", "Twelve",
    "Thirteen",
    "Fourteen", "Fifteen" };

    Are the two approaches exactly identical, or is there any difference
    between them?

    Thanks,
    Masood
    /******************************************************
    ******************************************************/

    #include <stdio.h>


    char* mdTbl[3][5] = {
    {
    "One",
    "Two",
    "Three",
    "Four",
    "Five"
    },
    {
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten"
    },
    {
    "Eleven",
    "Twelve",
    "Thirteen",
    "Fourteen",
    "Fifteen"
    },
    };


    void
    print_array_element(int row, int column)
    {
    printf("%s\n", mdTbl[row][column]);
    }

    main()
    {
    print_array_element(2, 2);
    }
     
    , Jan 23, 2005
    #1
    1. Advertising

  2. pete Guest

    wrote:
    >
    > Hi,
    >
    > I have seen at least two ways to
    > initialize multi-dimensional arrays in C.
    > One of the ways is shown in a sample code snippet below. The other
    > way does not make use of any intermediate braces. In other words, all
    > the entries are listed under the same pair of enclosing braces. For
    > example:
    >
    > char* mdTbl[3][5] = { "One", "Two", "Three", "Four","Five", "Six",
    > "Seven",
    > "Eight", "Nine", "Ten", "Eleven", "Twelve",
    > "Thirteen",
    > "Fourteen", "Fifteen" };
    >
    > Are the two approaches exactly identical,


    Yes.
    The left most brackets may be left empty in an array initialization.

    char* mdTbl[][5] = {
    "One", "Two", "Three", "Four","Five", "Six",
    "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
    "Thirteen","Fourteen", "Fifteen"
    };

    --
    pete
     
    pete, Jan 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Venkat
    Replies:
    4
    Views:
    1,014
    Venkat
    Dec 5, 2003
  2. Grumble

    Multi-dimensional array initialization

    Grumble, Jan 26, 2004, in forum: C Programming
    Replies:
    4
    Views:
    715
    Jack Klein
    Jan 27, 2004
  3. Replies:
    3
    Views:
    496
  4. Replies:
    3
    Views:
    405
    CBFalconer
    Jan 23, 2005
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    228
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page