question of null initialized char array

Ok so I have created a class which has the following form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Step
{
public:

	char plus[20];
	char particle[20];
	float energy_sec;
	float xpos_sec;
	float ypos_sec;
	float zpos_sec;

	Step()
	{
		plus[20] = NULL;
		particle[20] = NULL;
		energy_sec = 0;
		xpos_sec = 0;
		ypos_sec = 0;
		zpos_sec = 0;
	}


};


My main program assigns values to some of these parameters. In parts of my code I want to execute commands based on whether some of these parameters have been assigned values. This is easy for the float variables, for example, as I can write:

1
2
3
4
5
6
7
8
9
10
11
Step Trk;
Trk.xpos = ...
Trk.ypos = ...
.
.
. 

if(TRK.xpos = 0)
{
...
}


However when I am checking the character arrays to see if they have been assigned values im not sure what to write. Would I write

1
2
3
4
if(Trk.particle = NULL)
{
...
}


or

1
2
3
4
if(Trk.particle = 0)
{
...
}


or

1
2
3
4
if(Trk.particle = "")
{
...
}


I have tried all of the above and none really seem work. It seems like the if statement never executes in my code whether or not the argument statement is true or false. What am I doing wrong???
your if statements should have 2 ='s

if(something == somecheck)

you are assigning values in your statements which is incorrect, you need to validate using 2 equals :)
Last edited on
In your constructor you write to unnalocated memory, outside of array bounds. The program could crash.
modoran,

So how should I initialize my char variables in my constructor? Maybe something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Step
{
public:

	char plus[20];
	char particle[20];
	float energy_sec;
	float xpos_sec;
	float ypos_sec;
	float zpos_sec;

	Step()
	{
		plus[20] = "null";
		particle[20] = "null";
		energy_sec = 0;
		xpos_sec = 0;
		ypos_sec = 0;
		zpos_sec = 0;
	}


};


Then if I wanted to know if the variable was assigned a value or not I could write

1
2
3
4
5
if(Trk.particle == "null")
{
...
}


That does not seem to work either. When I use this method I get an error:


Error 1 error C2440: '=' : cannot convert from 'const char [5]' to 'char [20]'
Last edited on
Topic archived. No new replies allowed.