Solved

2 Dimensional Char Array Initialization - C Language

Posted on 2007-03-31
6
9,927 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
The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.

919 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

19 Experts available now in Live!

Get 1:1 Help Now