473,883 Members | 2,092 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
15 26446
Pete Becker wrote:
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.
Huh? When initializing an aggregate, all members get
initialized.

In this particular case, "" has identical meaning to { '\0' },
and all members of the array will be set to 0.

Sep 8 '06 #11
Old Wolf wrote:
Pete Becker wrote:
>>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.


Huh? When initializing an aggregate, all members get
initialized.

In this particular case, "" has identical meaning to { '\0' },
and all members of the array will be set to 0.
Citation, please? 8.5.2 does not say that. This is not "aggregate
initialization, " so 8.5.1 does not apply.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
Sep 8 '06 #12
Pete Becker wrote:
Old Wolf wrote:
>Pete Becker wrote:
>>>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.


Huh? When initializing an aggregate, all members get
initialized.

In this particular case, "" has identical meaning to { '\0' },
and all members of the array will be set to 0.

Citation, please? 8.5.2 does not say that.
I just read 8.5.2. It says: successive characters of the string-literal
initialize the members of the array; but I cannot find anything therein
that specifies what happens if the character array is longer than the
string literal (including the terminating 0). Is it up to the
implementation?
This is not "aggregate initialization, " so 8.5.1 does not apply.
That, I can see.
Best

Kai-Uwe Bux
Sep 8 '06 #13
Kai-Uwe Bux wrote:
>
I just read 8.5.2. It says: successive characters of the string-literal
initialize the members of the array; but I cannot find anything therein
that specifies what happens if the character array is longer than the
string literal (including the terminating 0). Is it up to the
implementation?
Seems like it. If the char array is initialized from a literal string
then it's holding a C-style string, and there's no reason to look at
anything after the terminating null character.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
Sep 8 '06 #14
Pete Becker wrote:
Old Wolf wrote:
Pete Becker wrote:
>hankssong wrote:
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.
Huh? When initializing an aggregate, all members get
initialized.

Citation, please? 8.5.2 does not say that. This is not "aggregate
initialization, " so 8.5.1 does not apply.
You're right that 8.5.1 doesn't apply (despite this being the
initialization of an aggregate), and 8.5.2 doesn't mention the
characters after the 0-terminator.

I notice that 8.5.1#4 appears to conflict with 8.5.2#1. It says:

An array of unknown size initialized with a brace
enclosed initializerlist containing n initializers, where n
shall be greater than zero, is defined as having n
elements (8.3.4).

If the code is:
char *array[] = { "string" };

then it's clear that { "string" } is a brace-enclosed initializer
list containing 1 initializer, and array is defined as having
1 element. So, according to 8.5.1#4, the code
char array[] = { "string" };

should also try to define array as having 1 element, and
initialize that element with a string literal, which should
cause an error since a string literal isn't a valid initailizer
for a single char -- in exactly the same way that
int array[] = { "string" };

fails. I think 8.5.1#4 should have an explicit mention that
this clause doesn't apply to initialization of character arrays
from a string literal.

Sep 11 '06 #15
Old Wolf wrote:
>
If the code is:
char *array[] = { "string" };

then it's clear that { "string" } is a brace-enclosed initializer
list containing 1 initializer, and array is defined as having
1 element. So, according to 8.5.1#4, the code
char array[] = { "string" };
8.5.1 doesn't apply to initialization from a string literal. 8.5.2
controls that. The first rule of statutory construction is "begin at the
beginning." In this case, the beginning is the opening paragraphs of
8.5, in particular, 8.5/14.
Sep 12 '06 #16

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
43480
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
2
48803
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
11157
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...
0
10763
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10863
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
10422
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...
0
7136
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
5807
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
6006
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.