Solved

2 Dimensional Char Array Initialization - C Language

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

char sqID_split[5][15] = {""};
0
Comment
Question by:s_more
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

809 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