Problem with setting an array of chars

Good morning,

I have a "Cell" class, with a "name" attribute. On other hand, I've got the main class with a bucle that generate a new array of chars, and then try to set the "name" of Cell with this value, but it doesn't run successfully.

Next code runs right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int i = 0;
	
	while ( i < 8 )
	{
		if ( i % 2 == 0 )
		{
			board[0][i].setName ( (char*) "Parell" );
		}
		else
		{
			board[0][i].setName ( (char*) "Imparell" );
		}
		
		printf ( "%dº. %s\n", i, board[0][0].getName() );
		i++;
	}


The output is:

0º. Parell
1º. Parell
2º. Parell
3º. Parell
4º. Parell
5º. Parell
6º. Parell
7º. Parell

Is right beacuse the app always must show the value of board[0][0] name.

But, curiously, the next code doesn't run successfully:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	int i = 0;
	
	while ( i < 8 )
	{
		char nom[] = "";
		nom[0] = 'X';
		nom[1] = (char) ( (int) '0' ) + i;
		char* name = (char*) nom;

		if ( i % 2 == 0 )
		{
			board[0][i].setName ( name );
		}
		else
		{
			board[0][i].setName ( (char*) "Imparell" );
		}
		
		printf ( "%dº. %s\n", i, board[0][0].getName() );
		i++;
	}


The output, on this case, is:

0º. X0
1º. X1
2º. X2
3º. X3
4º. X4
5º. X5
6º. X6
7º. X7

I don't know why, on the 2nd case, the value of board[0][0] name is modified.
the problem is on line 7 in your second code. nom has only one field, but you're trying to set the second.

You can write it like so:
char name [] = { 'X', (char) ( (int) '0' ) + i, 0 };
Thank you coder777.

I'm trying to set this code, but the problem persists:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	int i = 0;
	
	while ( i < 8 )
	{
		char name [] = { 'X', (char) ( (int) '0' ) + i, 0 };
			
		if ( i % 2 == 0 )
		{
			board[0][i].setName ( name );
		}
		else
		{
			board[0][i].setName ( (char*) "Imparell" );
		}
		
		printf ( "%dº. %s\n", i, board[0][0].getName() );
		i++;
	}


The output is the same:

0º. X0
1º. X1
2º. X2
3º. X3
4º. X4
5º. X5
6º. X6
7º. X7
Please show your implementation for getName and setName functions.
Cell.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Cell.h"

Cell::Cell ()
{
	stable = 0;
}

char* Cell::getName()
{
	return name;
}

void Cell::setName ( char* new_name )
{
	name = new_name;
}

int Cell::getStable()
{
	return stable;
}


Cell.h:

1
2
3
4
5
6
7
8
9
10
11
12
class Cell
{
	private:
		char* name;
		int stable;
	
	public:
		Cell ();
		char* getName();
		void setName ( char* new_name );
		int getStable();
};
I think the problem is that you are not assigining the value of the string to name but its address. The value in that address keeps changing during the while loop iteration.

 
name = new_name; // name now points to whatever new_name was pointing. 


Instead I would reccomend you not to use pointers in your class implementation. If you do use them, you will have to very carefully manage dynamic memory allocation / destruction.
And then, there is an "Othello" class.

Othello.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include "Othello.h"

void Othello::initBoard()
{
	int i = 0;
	
	while ( i < 8 )
	{
		char name [] = { 'X', (char) ( (int) '0' ) + i, 0 };
			
		if ( i % 2 == 0 )
		{
			board[0][i].setName ( name );
		}
		else
		{
			board[0][i].setName ( (char*) "Imparell" );
		}
		
		printf ( "%dº. %s\n", i, board[0][0].getName() );
		i++;
	}
}


Othello.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Cell.cpp"

class Othello
{	
	public:
		const static int EMPTY = 0;
		const static int BLACK = 1;
		const static int WHITE = 2;
		
		const static int USER = 0;
		const static int PC = 1;
		
		const static int MININT = -20000;
		const static int MAXINT = 20000;
		
		Cell board[8][8];
	
		void initBoard();
};
And which is the solution? Because the function getName can't return an array of chars without a pointer I think. The name has to be a string, but I don't know how it's possible without pointers.
you can use std::string instead of char*.

However, in your application it seems that the name will store the coordinates of the particular Cell. If such is the case, it can be implemented simply by member variables xCoord and yCoord instead of the compund name "name".
I'm trying to set this code, but the problem persists:
Yes, since name in your class Cell points in this case to the local variable name. So you will always see the content of the local variable.

you need to copy the content of the name not just the pointer
Thank you very much to both!! I'll use 2 variables, it's the best solution, and if I need use other Strings, it will better to use std::string.
Last edited on
Topic archived. No new replies allowed.