Solved

2 Dimensional Char Array Initialization - C Language

Posted on 2007-03-31
6
9,923 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:
Kdo 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now