Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without having more of your source posted, it looks and sounds like you are confusing arrays and strings.</p> <p>In C and C++ a 'char' is an 8-bit value that is usually used to store an ASCII character. The ASCII table is like the periodic table, it provides a numbered list of things so that you can refer to them numerically. 1 in the periodic table is Hydrogen, it is element 1. In ASCII the value 32 represents the space character, the value 48 represents the '0' character, 49 represents '1'.</p> <p>In C and C++ the convention is that if a sequence of characters are supposed to be treated as a string, they must end with a character that has the ASCII value of 0 (not '0', but 0, also written '\0').</p> <p>So store a 3-character string, you need 4 chars or bytes.</p> <pre><code>char foo[3] = "foo"; // illegal. "foo" is actually { 'f', 'o', 'o', 0 }; char bar[4] = "foo"; // ok </code></pre> <p>Because your arrays appear to be char-arrays and not strings, you cannot use "strcpy" etc, you must use "memcpy" or copy elements by hand.</p> <p>Here is a working version of the problem you are trying to solve, hope this helps.</p> <p>Online demo at ideone: <a href="http://ideone.com/6TcapX" rel="nofollow">http://ideone.com/6TcapX</a></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #define MAZE_COLUMNS 3 #define MAZE_ROWS 3 #define MAZEW_COLUMNS 4 #define MAZEE_COLUMNS 4 static void transcribeMazeRow(const char* source, size_t srcColumns, char prefix, char* dest, size_t destColumns) { dest[0] = prefix; memcpy(&amp;dest[1], &amp;source[0], srcColumns * sizeof(source[0])); } int main(int argc, char* argv[]) { // 3 rows of 3 columns, each is a distinct char. this is not a string. char maze[MAZE_ROWS][MAZE_COLUMNS] = { { 'a', 's', 'd' }, { 'u', 's', 'd' }, { 'p', 's', 'd' } }; // 3 rows of 4 columns, distinct character values, not a string. char mazeW[MAZE_ROWS][MAZEW_COLUMNS]; char mazeE[MAZE_ROWS][MAZEE_COLUMNS]; for (size_t row = 0; row &lt; MAZE_ROWS; ++row) { transcribeMazeRow(maze[row], MAZE_COLUMNS, 'W', mazeW[row], MAZEW_COLUMNS); transcribeMazeRow(maze[row], MAZE_COLUMNS, 'E', mazeE[row], MAZEE_COLUMNS); } // this part is mostly to show the poster the correct way to refer to all elements of each array. printf("maze: %c%c%c, %c%c%c, %c%c%c\n", maze[0][0], maze[0][1], maze[0][2], maze[1][0], maze[1][1], maze[1][2], maze[2][0], maze[2][1], maze[2][2] ); printf("mazeW: %c%c%c%c, %c%c%c%c, %c%c%c%c\n", mazeW[0][0], mazeW[0][1], mazeW[0][2], mazeW[0][3], mazeW[1][0], mazeW[1][1], mazeW[1][2], mazeW[1][3], mazeW[2][0], mazeW[2][1], mazeW[2][2], mazeW[2][3] ); printf("mazeE: %c%c%c%c, %c%c%c%c, %c%c%c%c\n", mazeE[0][0], mazeE[0][1], mazeE[0][2], mazeE[0][3], mazeE[1][0], mazeE[1][1], mazeE[1][2], mazeE[1][3], mazeE[2][0], mazeE[2][1], mazeE[2][2], mazeE[2][3] ); return 0; } </code></pre> <p>Output:</p> <pre><code>maze: asd, usd, psd mazeW: Wasd, Wusd, Wpsd mazeE: Easd, Eusd, Epsd </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload