473,890 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can i initialize a char array variable like this

char a[256] = { 0 }
is it ok?

Sep 7 '06 #1
15 26447
Flo
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in

char a[3] = { 'a', 'b', 0 } ;

For your huge array, i would use a loop or memset to initialize it.

Greetings

Flo

Sep 7 '06 #2
Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do.
Not so. All other elements will be _default initialized_ which is not
the same as _uninitialized_ . The default initalizer for built-in types
is zero.

Regards,
Bart.

Sep 7 '06 #3
Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, [...]
That is incorrect. All elements that are not given an explicit
initialiser are *zero-initialised*.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '06 #4
Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized,
That is simply not true, all of the other elements are default
initialized; which for integer types (like char) means that they are all
initialized to zero. If your compiler does not do this, then it is broken.

In the following:

char a[256] = {1};

The first element is initialized to (1), while all others are
initialized to (0).
what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in

char a[3] = { 'a', 'b', 0 } ;

For your huge array, i would use a loop or memset to initialize it.
I wouldn't.
--
Clark S. Cox III
cl*******@gmail .com
Sep 7 '06 #5
Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized,
really?

[8.5.1/7] If there are fewer initializers in the list than there are members
in the aggregate, then each member not explicitly initialized shall be
value-initialized (8.5).

[8.5/5] ...
To value-initialize an object of type T means:
? if T is a class type (clause 9) with a user-declared constructor (12.1),
then the default constructor for T is called (and the initialization is
ill-formed if T has no accessible default constructor);
? if T is a non-union class type without a user-declared constructor, then
every non-static data member and base-class component of T is
value-initialized;
? if T is an array type, then each element is value-initialized;
? otherwise, the object is zero-initialized

what is probably not what you have intended to do. If you like to
initialize the whole array, you have to initialize each element as in

char a[3] = { 'a', 'b', 0 } ;

For your huge array, i would use a loop or memset to initialize it.

Best

Kai-Uwe Bux

Sep 7 '06 #6
if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";

thinktwice 写道:
char a[256] = { 0 }
is it ok?
Sep 7 '06 #7
hankssong wrote:
>
>>char a[256] = { 0 }
is it ok?

if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";
That will set the first character to 0, which is fine if that's what's
needed. It's different from the first version, which sets all the
characters to 0.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
Sep 7 '06 #8
Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do.
Untrue! When you specify fewer initializers to an aggregate than there
are elements, the rest are default initialized.
>
Sep 7 '06 #9

Flo wrote:
Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in

char a[3] = { 'a', 'b', 0 } ;

For your huge array, i would use a loop or memset to initialize it.
WRONG!!!

Ok, I have nothing new to add, I just wanted to join in with the rest
of the hecklers and make sure you understand how very mistaken you
really are.

Sep 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
38262
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
10
43481
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
2
48805
by: Bill Todd | last post by:
What is the easiest way to convert a char array to string? -- Bill
0
1951
by: Kueishiong Tu | last post by:
How do I initialize a char array with null characters? Is there an equivalent function to bzero() in VC++.net?
2
6451
by: Augusto Cesar | last post by:
Hello people. How can I Pass ASP Array variable to Javascript; I´m to trying this: <script language="JavaScript"> var x = new Array(10);
13
6260
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things get me, which brings me to... I'm trying to create a program that gets a string from standard input and then manipulates it a little bit. It has to be a char array and not use string from the library. Here are my prototypes:
2
7189
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd appreciate any help or pointers here. This is the code, nothing fancy, just basic experiments around char array and string, #include <stdio.h> #include <string.h> int main() {
11
2343
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
9
7286
by: ashokd001 | last post by:
Hi How do i reinitialize a char array in one line if i want to use the same array variable every time. Ex- char name; -Ashok
0
9980
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.