Programming in C++: 3D array string copy

Click For Summary

Discussion Overview

The discussion centers around the challenge of copying a C-style string into a 3D array while developing a Klondike style solitaire game in C++. Participants explore the technical details of array manipulation and string handling in C++, particularly focusing on the implementation of a string copy function.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their attempt to copy a string into a 3D array and the compiler error they encountered, indicating a type mismatch.
  • Another participant suggests using the strcpy function from the C standard library as a more efficient method for copying strings.
  • There is a discussion about the need for an additional index in the destination array to properly store the string.
  • A later reply proposes a modified version of the strCopy function that includes parameters for the number of rows and columns, aiming to simplify the copying process.
  • One participant expresses gratitude for the assistance received and indicates that they have resolved their initial misunderstanding regarding array assignments.
  • Another participant shares their intention to implement a "card" struct in their program, which would change their approach to representing the tableau, suggesting they may no longer need a 3D array.

Areas of Agreement / Disagreement

Participants generally agree on the utility of using the strcpy function for string copying, but there is no consensus on the necessity of the 3D array structure for the tableau, as one participant proposes a different approach using structs.

Contextual Notes

There are unresolved aspects regarding the implementation details of the modified strCopy function, particularly concerning the handling of array dimensions and the proper use of indices.

Who May Find This Useful

This discussion may be useful for individuals learning about C++ array manipulation, string handling, and struct implementation in the context of game development.

Dembadon
Gold Member
Messages
660
Reaction score
88

Homework Statement



I am making a Klondike style solitaire game, and I am trying to load a string into a 3D array. I am representing the "Tableau" with a 3D array (we haven't gone over structs yet). I want to represent the spaces which don't have cards with the word "empty". I'm not understanding how to copy a C-style string to a 3D array.

Homework Equations



My compiler, g++, is giving me the following error:

Code:
uploadTest.cpp: In function ‘void strCopy(char (*)[60][80], char*)’:
uploadTest.cpp:240: error: incompatible types in assignment of ‘char’ to ‘char [80]’

Here are my array declarations:
Code:
char emptyRow[ MAX_STR_LEN ] = "empty";
char tableau[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ];

Here's how it is called:
Code:
strCopy( tableau, emptyRow );

The Attempt at a Solution



Code:
// prototype
void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] );

////////////////////////////////////////////////////////////////

// implementation

void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] ) {
	
	// initialize function/variables
	int colI = 0;
	int rowIndex = 0, colIndex = 0;
	
	// loop through source array
	while( srcArr[ colI ] != NULL_CHAR ) {
		
		// assign character
		destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];
		
		// increment index
		colI++;
	}
	
}

Do I have to create a third index for the destArr and increment it? I tried doing this but the compiler still complained.
 
Physics news on Phys.org
Dembadon said:

Homework Statement



I am making a Klondike style solitaire game, and I am trying to load a string into a 3D array. I am representing the "Tableau" with a 3D array (we haven't gone over structs yet). I want to represent the spaces which don't have cards with the word "empty". I'm not understanding how to copy a C-style string to a 3D array.

Homework Equations



My compiler, g++, is giving me the following error:

Code:
uploadTest.cpp: In function ‘void strCopy(char (*)[60][80], char*)’:
uploadTest.cpp:240: error: incompatible types in assignment of ‘char’ to ‘char [80]’

Here are my array declarations:
Code:
char emptyRow[ MAX_STR_LEN ] = "empty";
char tableau[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ];

Here's how it is called:
Code:
strCopy( tableau, emptyRow );

The Attempt at a Solution



Code:
// prototype
void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] );

////////////////////////////////////////////////////////////////

// implementation

void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] ) {
	
	// initialize function/variables
	int colI = 0;
	int rowIndex = 0, colIndex = 0;
	
	// loop through source array
	while( srcArr[ colI ] != NULL_CHAR ) {
		
		// assign character
		destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];
		
		// increment index
		colI++;
	}
	
}

Do I have to create a third index for the destArr and increment it? I tried doing this but the compiler still complained.

Instead of copying characters one by one, which appears to be what you think you're doing, why not use the strcpy C standard library function?

Here's where your error is coming from, I believe.
Code:
destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];

The problem is a mismatch of types. On the left side, destArr[ rowIndex ][ colIndex ] is an address, the address of the first character in the string it will hold. On the right side is a character. This is what your compiler error message is saying.


Also, since your tableau array is essentially a two-dimensional array of strings, I would use an outer for loop to iterate through the rows, and an inner for loop to iterate through the columns in a particular row. Presumably you know how many rows and columns there will be.

Here's how I would do what you're trying to do. Notice that I added a couple of parameters for the number of rows and columns of the tableau. This should work, but I haven't had time to test it.

Code:
void strCopy( char destArr[ ][ ], srcArr[ ], int rows, int cols )
{
   int i, j;
   for (i = 0; i < rows, ++i)
   {
      for (j = 0; j < cols; ++j)
      {
         strcpy(destArr[i][j], srcArr);
      }
}
 
Thank you for your help, Mark! I now understand my error with array assignments, and the strCopy function you provided worked.

Additionally, we went over structs today in class, and I think I can already find a good use for them in this program. I'm planning on making a "card" struct, which will hold three elements: rank, suit, color. I can then declare a deck array of size 52, load it from a file, and use then use it to populate my tableau. I also don't think I'll need a 3D array for the tableau anymore, since I should just be able to load the tableau at a given row and column using my newly created deck array.
 
Dembadon said:
Thank you for your help, Mark! I now understand my error with array assignments, and the strCopy function you provided worked.
Sure, you're welcome, and glad to hear that it worked for you.
Dembadon said:
Additionally, we went over structs today in class, and I think I can already find a good use for them in this program. I'm planning on making a "card" struct, which will hold three elements: rank, suit, color. I can then declare a deck array of size 52, load it from a file, and use then use it to populate my tableau. I also don't think I'll need a 3D array for the tableau anymore, since I should just be able to load the tableau at a given row and column using my newly created deck array.
Sounds good!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
20K
Replies
8
Views
4K