Multi-dimensional array initialization

M

masood.iqbal

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

Eric Sosman

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.
 
E

Eric Sosman

Eric said:
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.
 
K

kiru.sengal

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top