简体   繁体   English

扩展二维矩阵

[英]Extending a 2d matrix

I have input matrix maze[3][3] as 我输入矩阵迷宫[3] [3]为

asd
usd
psd

and I want to convert it into two matrix mazeW[3][4] and mazeE[3][4] of form 我想将其转换为两个矩阵mazeW [3] [4]和mazeE [3] [4]

Wasd 
Wusd
Wpsd

and

asdE
usdE
psdE

How should I do it?? 我应该怎么做? I tried this but not helping 我尝试了这个但没有帮助

for(int i=0;i<row;i++)
{
strcpy("W",strcpy(mazeW[i],maze[i]));
strcpy(strcpy(mazeE[i],maze[i]),"E");
}

If you are using strcpy function, your source string must terminate with '\\0', Seems like in your code maze[3][3] is not intended to be a array of strings as there is no space reserved for '\\0'. 如果您使用的是strcpy函数,则源字符串必须以'\\ 0'结尾,似乎在您的代码迷宫[3] [3]中不是字符串数组,因为没有为'\\ 0'保留空间。

In that case you cat use strcpy function instead use memcpy where you can provide the size of memory to be copied. 在那种情况下,您可以使用strcpy函数而不是memcpy ,在这里您可以提供要复制的内存大小。

for ( i=0; i < row; i++ )
{
    mazeW[i][0] = 'W';
    memcpy ( mazeW[i]+1, maze[i], 3); 

    mazeE[i][3] = 'E';
    memcpy ( mazeE[i], maze[i], 3);
}
for(i=0;i<3;i++)
{
    mazeW[i][0] = 'W';  //sets the first element of each line to W
    memcpy(&mazeW[i][1], maze[i], sizeof(char)*3);   //copies the rest of the line from maze

    mazeE[i][3] = 'E';  // sets the last element of each line to E
    memcpy(&mazeE[i][0], maze[i], sizeof(char)*3);  ////copies the beggining of the line from maze
}

Without having more of your source posted, it looks and sounds like you are confusing arrays and strings. 在没有发布更多源代码的情况下,看起来和听起来像是您在混淆数组和字符串。

In C and C++ a 'char' is an 8-bit value that is usually used to store an ASCII character. 在C和C ++中,“ char”是8位值,通常用于存储ASCII字符。 The ASCII table is like the periodic table, it provides a numbered list of things so that you can refer to them numerically. ASCII表就像元素周期表一样,它提供了一个编号列表,以便您可以用数字方式引用它们。 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'. 元素周期表中的1是氢,是元素1。在ASCII中,值32表示空格字符,值48表示“ 0”字符,值49表示“ 1”。

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'). 在C和C ++中,约定是:如果假定将字符序列视为字符串,则它们必须以ASCII值0(不是'0',而是0,也写为'\\ 0')结束)。

So store a 3-character string, you need 4 chars or bytes. 因此,存储一个3个字符的字符串,则需要4个字符或字节。

char foo[3] = "foo"; // illegal. "foo" is actually { 'f', 'o', 'o', 0 };
char bar[4] = "foo"; // ok

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. 因为您的数组看起来是字符数组而不是字符串,所以不能使用“ strcpy”等,因此必须使用“ memcpy”或手动复制元素。

Here is a working version of the problem you are trying to solve, hope this helps. 这是您要解决的问题的有效版本,希望对您有所帮助。

Online demo at ideone: http://ideone.com/6TcapX ideone上的在线演示: http ://ideone.com/6TcapX

#include <stdio.h>
#include <string.h>

#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(&dest[1], &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 < 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;
}

Output: 输出:

maze: asd, usd, psd
mazeW: Wasd, Wusd, Wpsd
mazeE: Easd, Eusd, Epsd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM