char a[256] = { 0 }
is it ok? 15 26318
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
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.
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
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
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
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?
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.
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.
>
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.
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.
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.
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
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Yang Lee |
last post by:
hi
is this correct
char teststr="\0";
or is there another method to initialise;
thanks
lee
|
by: Bill Todd |
last post by:
What is the easiest way to convert a char array to string?
--
Bill
|
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?
|
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);
|
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...
|
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...
|
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;
};
|
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
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |