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. Eric Sosman 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, or is there any difference
    > between them?


    Both approaches initialize the array's content to the
    same values, so they are identical from that perspective.

    The "fully-braced" style is usually preferable, because
    the compiler may be able to use the extra information to give
    better diagnostics for missing or extraneous initializers.
    For example, in

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

    .... many compilers will warn you that there are fewer
    initializers than array elements -- but where is the
    problem? It's not too hard to spot the error in this
    small piece of code, but in a long list it could be more
    than a little tedious to find the mistake.

    Supplying the "internal" braces gives

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

    .... and a helpful compiler will tell you not only that
    there are too few initializers, but that there are too
    few initializers for the mdTbl[2] sub-array. This could
    make your search for the error considerably shorter.

    It's almost always a good idea to tell the compiler
    (or assembler, or linker, or program-generation tool) all
    you can about your code, because the tool may sometimes
    be able to detect and report conflicts, drawing your
    attention to an error before you spend hours and hours
    debugging it. That's why you #include the header that
    declares a function in the source module that defines
    the function: it makes no difference to the meaning of
    the program, but gives the compiler a chance to catch any
    accidental mismatch between definition and declaration.
    The same principle is at work here.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 23, 2005
    #2
    1. Advertising

  3. Eric Sosman Guest

    Eric Sosman wrote:
    >
    > Supplying the "internal" braces gives
    >
    > char *mdTbl[3][5] = {
    > { "One", "Two", "Three" },
    > { "Four", "Five", "Six" },
    > { "Seven", "Eight," "Nine" },
    > { "Ten", "Eleven", "Twelve" },
    > { "Thirteen", "Fourteen", "Fifteen" } };


    Woops! That's backwards, or perhaps inside-out.
    What I *meant*, of course, was

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

    The rest of my diatribe stands -- in fact, I only realized
    my mistake when I ran the original through a compiler and
    got a diagnostic message I wasn't expecting. Take it as an
    object lesson in the value of telling the compiler all you can.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 23, 2005
    #3
  4. 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:


    There is no difference in underlying initialization when you are
    intending to explicitly initialize every element of your
    mulitidimensional array. If you only want to explicity initialize a
    subset of the over multidimensional array you are defining, then there
    is a difference in the two methods. A single all encompassing set of
    braces will treat the multidimensional array as a contiguous set of
    elements when initializing. Nested braces will treat each set of
    nested brace as initializations for subset arrays of your larger
    multidimensional array.

    Try running this program (it's google-indent safe; shamely uses /**/) :

    #include<stdio.h>

    /**/int main(void)
    /**/{
    /**/ int i, j;

    /**/ int a[4][4]={ 1 , 2, 3, 4, 5, 6, 8};
    /**/ int b[4][4]={ {1, 2} , {3, 4} , {5, 6} , {7 ,8} };


    /**/ for(i = 0; i < 4; i++)
    /**/ {
    /**/ putchar('\n');
    /**/ for(j = 0; j < 4; j++)
    /**/ {
    /**/ printf("a[%d][%d] = %d\n", i, j, a[j]);
    /**/ }
    /**/ }

    /**/ putchar('\n');

    /**/ for(i = 0; i < 4; i++)
    /**/ {
    /**/ putchar('\n');
    /**/ for(j = 0; j < 4; j++)
    /**/ {
    /**/ printf("b[%d][%d] = %d\n", i, j, b[j]);
    /**/ }
    /**/ }

    /**/ putchar('\n');

    /**/ return 0;
    /**/}

    If google doesn't clean up it's act soon, I'm redownloading my news
    reader app :p
    , Jan 23, 2005
    #4
    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:
    959
    Venkat
    Dec 5, 2003
  2. Grumble

    Multi-dimensional array initialization

    Grumble, Jan 26, 2004, in forum: C Programming
    Replies:
    4
    Views:
    689
    Jack Klein
    Jan 27, 2004
  3. Replies:
    1
    Views:
    297
  4. Replies:
    3
    Views:
    377
    CBFalconer
    Jan 23, 2005
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page