Solved

2 Dimensional Char Array Initialization - C Language

Posted on 2007-03-31
6
9,952 Views
Last Modified: 2008-01-09
Is this initialization correct ?

char sqID_split[5][15] = {""};
0
Comment
Question by:s_more
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 25 total points
ID: 18828734

Hi s_more,

That's one possible initialization.  Though all it does is initialize the first element.

The declaration of the array:

  char sqID_split[5][15]

quite literally sets aside 75 characters (5 * 15) for the array.  The index definition of [5][15] tells the compiler that the block is actually 5 items of 15 bytes in length.

An initialization of the array as you've done (""} writes and end-of-string into the first byte of the first 15 character string.  You're literally putting a zero into the first byte of the array and leaving the rest unititialized.  If the array is in the globals block, the array will, by default, contain all zeros so this isn't actually necessary.  But it is good practice!

I'd suggest that your initialization look like this:

  char sqID_split[5][15] = {"", "", "", "", ""};

That way you're explicitly declaring that all 5 strings are empty.


Good Luck,
Kent
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 25 total points
ID: 18828979
>> That's one possible initialization.  Though all it does is initialize the first element.

If the compiler follows the specifications, then the other elements in the array will be initialized implicitly to 0. So, { "" } is sufficient to set all strings in the array to "" (the empty string).
0
 
LVL 11

Expert Comment

by:cup
ID: 18830038
It does depend on how compliant your compilers are.  Sure, most of them will do the initialization correctly but some of the K&R ones or the ones targetted at low volume hardware may not do it.  You may instead have to resort to run time stuff like

memset (sqID_split, 0, sizeof (sqID_split));

Remember that presetting values in non-const variables may not always work in EPROMs.  I've been bitten lots of times by this.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:s_more
ID: 18833983
All,

I'm using Visual Studio 2003 and when I use:
char sqID_split[5][15] = {"t"}; // example

it sets first element to "t" but if I print second or third element it shows that it is blank
and there is no garbage value inside it.
why ???

s_more
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18834733
>> it sets first element to "t" but if I print second or third element it shows that it is blank

As I said earlier :

    "If the compiler follows the specifications, then the other elements in the array will be initialized implicitly to 0."

The first element will be initialized to "t", the others to 0 (as in string "")
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 18835055
Some compilers will do what you want if you do:

char sqID_split[5][15] = {"t",}; // example

Note the extra comma. Try it.

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question