Initialize an array inside a class

H

Hamburgpear

Dear,

I have a private variable of double array x[100], how can I initialize it to
'\0'? should I create a for loop in the constructor? or I can simply write
double x[100]={'\0'} in my class_name.h file?

Thanks
 
J

John Harrison

Hamburgpear said:
Dear,

I have a private variable of double array x[100], how can I initialize it to
'\0'? should I create a for loop in the constructor?

That would work, so would using the standard function fill_n.

#include <algorithm>

....

std::fill_n(x, 100, 0.0);


or I can simply write
double x[100]={'\0'} in my class_name.h file?

That's not legal syntax.

john
 
J

John Harrison

John said:
Hamburgpear said:
Dear,

I have a private variable of double array x[100], how can I initialize
it to '\0'? should I create a for loop in the constructor?

I just read this again, initialise a double array to '\0'. What do you
mean by that? It doesn't make any sense.

john
 
H

Hamburgpear

Dear John,

Yes, you are right, originally, I want to compare the array element so that
I will terminate the loop if array element = '\0'. I just tested it and
found that the loop terminate even it is 0. Any suggestion?

Thanks
Perry

John Harrison said:
John said:
Hamburgpear said:
Dear,

I have a private variable of double array x[100], how can I initialize
it to '\0'? should I create a for loop in the constructor?

I just read this again, initialise a double array to '\0'. What do you
mean by that? It doesn't make any sense.

john
 
M

makc.the.great

Hamburgpear said:
Yes, you are right, originally, I want to compare the array element so that
I will terminate the loop if array element = '\0'. I just tested it and
found that the loop terminate even it is 0. Any suggestion?

perhaps hamburg pear thinks that '\0' consists of two charactes '\' and
'0', and so it is "double character"?

well, neither is '\0' is "double character", nor "double x[*]" is.

'\0' is single zero byte.
 
K

Karl Heinz Buchegger

Hamburgpear said:
Dear John,

Yes, you are right, originally, I want to compare the array element so
that I will terminate the loop if array element = '\0'. I just tested it
and found that the loop terminate even it is 0. Any suggestion?

Thanks
Perry

Your descriptions get more and more confusing.
If you want help, there is one sure way for posting
such that everybody will understand what you are heading at
and what gives you problems:

p o s t c o d e


Oh, and btw: please don't top post. Put your reply beneath the text you are
replying to
and delete what you don't need in your reply. Thank you.
 
G

Greg Comeau

Hamburgpear wrote:
or I can simply write
double x[100]={'\0'} in my class_name.h file?

That's not legal syntax.

I don't know why they person would want to use '\0' "as" 0.0
for a double but actually, it's fine. It will explicity initialize
x[0] and implicitly intiialize the remaining 99 elements to 0.0
 
J

Jim Langston

Hamburgpear said:
Dear John,

Yes, you are right, originally, I want to compare the array element so
that I will terminate the loop if array element = '\0'. I just tested it
and found that the loop terminate even it is 0. Any suggestion?

'\0' IS 0.

'\0' is a way to represent the character zero (ASCII value 0).
another way to represent it is with 0
That is, the following two lines of code do the exact same thing:
char MyChar = '\0';
char MyChar = 0;

a character in C++ is an integer value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top