Solved

2 Dimensional Char Array Initialization - C Language

Posted on 2007-03-31
6
9,938 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
Industry Leaders: 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!

 

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 186
negation in C function 14 167
Finding a good hash function 4 130
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 83
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

679 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