2

I' trying to assign elements to (nXn) character array where at the ends is "M",and on remaining of the border is "F". The error is:

Segmentation fault :core dumped

My code is:

#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,j;
    printf("Enter the size of matrix:\n");
    scanf("%d",&n);
    char *mat[n][n];
    for(i=0;i<n;i++) //Reset matrix
    {
        for(j=0;j<n;j++)
        {
            strcpy(mat[i][j],"0");
        }
    }
    for(i=0;i<n;i++) //Diagonals
    {
        strcpy(mat[i][i],"M");
        strcpy(mat[n-1-i][i],"M");
    }
    for(i=1;i<n-1;i++) 
    {
        strcpy(mat[0][i],"F");//Top border
        strcpy(mat[i][0],"F");//Left border
        strcpy(mat[i][n-1],"F");//Right border
        strcpy(mat[n-1][i],"F");//Bottom border
    }
    return 0;
}

I'm new to programming and don't really know why this error is occuring. Any suggestion/help ?

7
  • 3
    You need to allocate memory for mat[i][j]. Commented Aug 21, 2017 at 13:18
  • Perhaps you want char mat[n][n][2]? Commented Aug 21, 2017 at 13:18
  • 3
    char mat[n][n]; then mat[X][Y] = '?'; instead of strcpy(mat[X][Y],"?"); Commented Aug 21, 2017 at 13:19
  • Seems like you really want a 2d array of characters, not pointers, and you want to assign the values with mat[i][j] = '0'; not strcpy. Commented Aug 21, 2017 at 13:20
  • 1
    What is the first strcpy in your program? Coping '0' into all of your items. I used an example from your code. mat[x][y] = 'X'; works just fine too. Commented Aug 21, 2017 at 13:22

2 Answers 2

3

You did not declare your matrix properly, and because of that you did not use the proper code to set it up.

If you wanted an N×N character array, not an N×N array of C strings, you should declare it without an asterisk:

char mat[n][n];

Now you can use plain assignment of character constants (note single quotes) to elements of mat, like this:

for(i=0;i<n;i++) {
    for(j=0;j<n;j++) {
        mat[i][j] = '0';
    }
}

If you wanted an N×N array of C strings, and you are OK with these strings always coming from string literals, you could use assignment in place of string copying as well:

char *mat[n][n];
for(i=0;i<n;i++) {
    for(j=0;j<n;j++) {
        mat[i][j] = "0"; // Double quotes are back
    }
}

Finally, if you want to use string functions, you would need to change the array to N×N×M, where M is the longest string that you would like your matrix to hold plus one for null terminator. If all string are of a single character, the way they are in your example, the declaration becomes

char mat[n][n][2];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. I was doing same but with double quotes. :)
0

Instead of a two-dimensional array of characters you declared a two-dimensional array of pointers to char.

char *mat[n][n];

And you should not use the function strcpy because the two-dimensional array will not contain strings.

I think you mean the following

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

int main(void) 
{
    size_t n;

    printf( "Enter the size of matrix: " );
    scanf( "%zu", &n );

    char mat[n][n];

    memset( mat, '0', n * n );

    for ( size_t i = 0; i < n; i++ )
    {
        mat[i][i] = 'M';
        mat[i][n - i - 1] = 'M';
    }

    for ( size_t i = 1; i < n - 1; i++ )
    {
        mat[0][i]   = 'F';
        mat[n-1][i] = 'F';
        mat[i][0]   = 'F';
        mat[i][n-1] = 'F';
    }

    putchar( '\n' );

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            printf( "%c", mat[i][j] );
        }

        putchar( '\n' );
    }

    return 0;
}

The program output might look the following way

Enter the size of matrix: 10

MFFFFFFFFM
FM000000MF
F0M0000M0F
F00M00M00F
F000MM000F
F000MM000F
F00M00M00F
F0M0000M0F
FM000000MF
MFFFFFFFFM

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )

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.