Thread: Matrix problem with pointers

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    7

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    7
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Matrix multiplication using pointers
    By NARENDRA.B in forum C Programming
    Replies: 4
    Last Post: 02-18-2014, 08:02 AM
  3. Problem with 'Matrix Multiplication using Pointers'
    By JennyG in forum C Programming
    Replies: 20
    Last Post: 07-30-2012, 12:58 PM
  4. Hi, problem with pointers and matrix multiplication
    By Phadelity in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 11:53 PM
  5. 2D matrix pointers regarding functions
    By Shotgun in forum C Programming
    Replies: 1
    Last Post: 04-02-2006, 01:09 AM

Tags for this Thread