# Thread: Matrix problem with pointers

1. ## Matrix problem with pointers

hi, i have a problem with pointers. I have declared a matrix that receives n x m lines and columns, but I receive a fault segment and I do not know why .. can you help me please?
Code:
```#include <stdio.h>
#include <stdlib.h>
int main(int **matrix, int matrixSize) {

int i,j,temp;
printf("%s","Enter size of matrix:");
scanf("%d",&matrixSize);

printf("%s","Enter matrix elements\n");

for(i=0;i<matrixSize;i++) {
for(j=0;j<matrixSize;j++) {
scanf("%d",&matrix[i][j]);
}
}
for(i=0; i<matrixSize/2; i++) {
for(j=i; j<matrixSize-i-1; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][matrixSize-1-i];
matrix[j][matrixSize-1-i] = matrix[matrixSize-1-i][matrixSize-1-j];
matrix[matrixSize-1-i][matrixSize-1-j] = matrix[matrixSize-1-j][i];
matrix[matrixSize-1-j][i] = temp;
}
}
for(i=0;i<matrixSize;i++) {
for(j=0;j<matrixSize;j++) {
printf("%d ",matrix[i][j]);}
printf("%s","\n");
}

return 0;
}```

2. Ugh, the first issue is that the main function doesn't have those parameters. You should declare them as local variables instead.

The second issue is that you talk about a n x m matrix, but it looks like you're trying to create a square matrix instead.

The third issue is that you don't actually allocate memory for your matrix. This is the most likely reason for the segmentation fault.

3. oh my mistake ..
First of all, I declared the variables inside exactly as you said and expected it but if I have a function like this int ** rotateMatrix (int ** matrix, int matrixSize) ?
how should I write that "main" in the function I wrote? how should i declare?
Code:
```    int **matrix[100][100];
int matrixSize;
int i,j,temp;
printf("%s","Enter size of matrix:");
scanf("%d",&matrixSize);

printf("%s","Enter matrix elements\n");

for(i=0;i<matrixSize;i++) {
for(j=0;j<matrixSize;j++) {
scanf("%d",&matrix[i][j]);
}
}
for(i=0; i<matrixSize/2; i++) {
for(j=i; j<matrixSize-i-1; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][matrixSize-1-i];
matrix[j][matrixSize-1-i] = matrix[matrixSize-1-i][matrixSize-1-j];
matrix[matrixSize-1-i][matrixSize-1-j] = matrix[matrixSize-1-j][i];
matrix[matrixSize-1-j][i] = temp;
}
}
for(i=0;i<matrixSize;i++) {
for(j=0;j<matrixSize;j++) {
printf("%d ",matrix[i][j]);}
printf("%s","\n");
}```

4. That main function could just look like this:
Code:
```int main(void)
{```
Note that you've changed matrix to be an array of array of pointers to pointers; you probably want it to be an array of array of ints instead.