Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multi-dimensional array initialization

Reply
Thread Tools

Multi-dimensional array initialization

 
 
masood.iqbal@lycos.com
Guest
Posts: n/a
 
      01-23-2005
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);
}

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-23-2005
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
(E-Mail Removed)lid

 
Reply With Quote
 
kiru.sengal@gmail.com
Guest
Posts: n/a
 
      01-23-2005
(E-Mail Removed) 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[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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments