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
 
 
 
 
pete
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,


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

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

--
pete
 
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