0

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. The segmentation fault occurs as soon as I enter the function. The first printf never executes.

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.

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

Whenever I try to use matrix or output or strcpy() I get a segmentation fault. I can use printf simply on str or temp, but that is all. All the lines commented out will cause a seg fault. Any help would be greatly appreciated!

5
  • 1
    When you pass the variable declared like this: int matrix[5][5]; to a function, it decays to a single pointer (int*) not an int**. It will point to the start of the block of data that is 'perceived' by the program as a 1D array of 25 integers. Commented Apr 18, 2020 at 18:45
  • 1
    Ref your commented-out // strcat(str, temp); you can't do that with a string literal. Apart from *str pointing to a 1-byte space, it is read-only. Commented Apr 18, 2020 at 19:02
  • 1
    Please post the Minimal Reproducible Example, the shortest complete code that shows the problem. Commented Apr 18, 2020 at 19:06
  • 1
    Please add fflush(stdout) after the printf debugging cue, to be sure it is printed. When a program crashes, buffred output is usually discarded. Back to my earlier comment, you can try char str[1024] = ""; instead, or longer, and guarded from overflow by using strncat. Commented Apr 18, 2020 at 19:09
  • Thanks this helped a lot! changing my string to not read-only fixed a lot of problems. Commented Apr 18, 2020 at 20:44

1 Answer 1

1

The argument is of type int (*)[5] and the parameter is of type int**, these are not compatible, use:

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.

You wouldn't need to use strcat to do this, you can assign these directly to output with a proper conversion:

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.