Multi-dimensional array initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • masood.iqbal@lycos.com

    #1

    Multi-dimensional array initialization

    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","Fiv e", "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_ele ment(int row, int column)
    {
    printf("%s\n", mdTbl[row][column]);
    }

    main()
    {
    print_array_ele ment(2, 2);
    }

  • pete

    #2
    Re: Multi-dimensional array initialization

    masood.iqbal@ly cos.com wrote:[color=blue]
    >
    > 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","Fiv e", "Six",
    > "Seven",
    > "Eight", "Nine", "Ten", "Eleven", "Twelve",
    > "Thirteen",
    > "Fourteen", "Fifteen" };
    >
    > Are the two approaches exactly identical,[/color]

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

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

    --
    pete

    Comment

    Working...