Multi-dimensional array initialization

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

    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);
    }

  • Eric Sosman

    #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, or is there any difference
    > between them?[/color]

    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
    esosman@acm-dot-org.invalid

    Comment

    • Eric Sosman

      #3
      Re: Multi-dimensional array initialization

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

      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
      esosman@acm-dot-org.invalid

      Comment

      • kiru.sengal@gmail.com

        #4
        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[/color]
        in[color=blue]
        > C. One of the ways is shown in a sample code snippet below. The[/color]
        other[color=blue]
        > way does not make use of any intermediate braces. In other words,[/color]
        all[color=blue]
        > the entries are listed under the same pair of enclosing braces. For
        > example:[/color]

        There is no difference in underlying initialization when you are
        intending to explicitly initialize every element of your
        mulitidimension al array. If you only want to explicity initialize a
        subset of the over multidimensiona l array you are defining, then there
        is a difference in the two methods. A single all encompassing set of
        braces will treat the multidimensiona l 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
        multidimensiona l 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[i][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[i][j]);
        /**/ }
        /**/ }

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

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

        If google doesn't clean up it's act soon, I'm redownloading my news
        reader app :P

        Comment

        Working...