简体   繁体   English

将二维数组连接成字符串导致分段错误

[英]Concatenating 2D array into string causing segmentation fault

I'm trying to concatenate a matrix into one long string using strcat , but keep getting seg faults whenever I attempt to access the matrix or use strcat .我正在尝试使用strcat将矩阵连接成一个长字符串,但每当我尝试访问矩阵或使用strcat时,都会出现段错误。 The segmentation fault occurs as soon as I enter the function.一进入function就会出现分段错误。 The first printf never executes.第一个printf永远不会执行。

void concatMatrix(int **matrix, char *output){ 
  printf("%s", "SDFSDFDSFDSFDSF");

  char *str = "";
  char *temp = "sdds";
  for(int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
       // temp = (char)matrix[i][j];
       // strcat(str, temp);
       // strcat(str, ' ');
       // printf("%d\n", matrix[i][j]);
    }
    // strcat(str, "\n");
    strcat(output, str);
    // printf("%s", output);
  }
}

This is how matrix and output were declared, and matrix was filled with values before calling the function.这就是矩阵和 output 的声明方式,矩阵在调用 function 之前填充了值。

int matrix[5][5];
char output[25];

Whenever I try to use matrix or output or strcpy() I get a segmentation fault.每当我尝试使用矩阵或 output 或strcpy()时,我都会遇到分段错误。 I can use printf simply on str or temp, but that is all.我可以简单地在 str 或 temp 上使用printf ,但仅此而已。 All the lines commented out will cause a seg fault.所有注释掉的行都会导致段错误。 Any help would be greatly appreciated!任何帮助将不胜感激!

The argument is of type int (*)[5] and the parameter is of type int** , these are not compatible, use:参数是int (*)[5]类型,参数是int**类型,这些不兼容,使用:

void concatMatrix(int matrix[][5], char *output);

Furthermore, strcat 's second parameter is expecting a char array and you are passing single char arguments to it, aside from the fact that str points to a string literal which is constant and cannot be altered.此外, strcat的第二个参数需要一个 char 数组,并且您将单个 char arguments 传递给它,除了str指向一个常量且无法更改的字符串文字这一事实。

You wouldn't need to use strcat to do this, you can assign these directly to output with a proper conversion:您不需要使用strcat来执行此操作,您可以通过适当的转换将这些直接分配给output

Running sample运行示例

#include <stdio.h>

void concatMatrix(int matrix[][5], char *output)
{  
    int index = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++, index++)
        {        
        output[index] =  matrix[i][j] + '0'; //convert from int to char and assign to output
        }       
    }
    output[index] = '\0'; //null terminate the string
}

int main()
{
    int matrix[5][5] = {{1, 4, 3, 5, 2},
                        {7, 9, 5, 9, 0},
                        {1, 4, 3, 5, 2},
                        {1, 4, 3, 5, 2},
                        {7, 9, 5, 9, 0}};
    char output[26]; //must have space for null terminator
    concatMatrix(matrix, output);
    printf("%s", output);
}

This will work only for single digits, wich, I gather, is the intended purpose given the size of the output string and the rest of the code.鉴于output字符串的大小和代码的 rest 的大小,这仅适用于单个数字。

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

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