Thread: Writing, passing, and handling matrices???

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Writing, passing, and handling matrices???

    I cannot do anything w/ matrices in C, and I cant find my mistake. Below is my code. It basically uses two functions, one to dynamically allocate a matrix, and another to initilaze the matrix w/ all zeros! The main program basically just allocates the all zero matrix, and then it prints it. This is a very small and simple program. It compiles w/ no warnings whatsoever. When I run the program it says access violation.

    If I do a[0][0], no problems, where a is a float**. If I do a[1][1] then I get access violation. The funny thing is I have seen tons and tons of code that takes a float** and accesses it as if it were a float[][]. So, I must be missing something.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    float** newmtrx(int, int); //Allocate space for matrix return pointer
    void initmtrx(float**, int, int); //set all entries to zero
    
    void main()
    {
        float** a;
        int i=0,j=0;
       
        a = newmtrx(5,5);  //allocate and point to 5x5 matrix
        initmtrx(a,5,5);  //set values in a to zero
       
        for(i=0; i<5; i++)
            for(j=0; j<5; j++)
                printf("%f\t",a[i][j]);
    } //end of main
    
    //function definitions
    float** newmtrx(int m, int n)
    {
      int i;
      float **p;
    
      p=(float **)malloc((unsigned) (m)*sizeof(float*));
      if (!p) printf("Failure in allocate_real_matrix().");
    
      for (i = 0; i < m; i++){
        p=(float **)malloc((unsigned) (n)*sizeof(float*));
        if (!p[i]) printf("Failure in allocate_real_matrix().");
      }
      return p;
    }
    
    void initmtrx(float** a, int m, int n)
    {
        for(;m>0;m--)
            for(;n>0;n--)
                a[m-1][n-1] = 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Replace
    Code:
          p=(float **)malloc((unsigned) (n)*sizeof(float*));
    with
    Code:
          p[i] = malloc(n * sizeof *p[i]);
    [edit]Also, just use the straightforward method for the initialization.
    Code:
    void initmtrx(float** a, int m, int n)
    {
       int i, j;
       for ( i = 0; i < m; ++i )
       {
          for ( j = 0; j < n; ++j )
          {
             a[i][j] = 0;
          }
       }
    }
    Otherwise, you lose the fact that your attempt at trickery doesn't reset n at each m loop.
    Last edited by Dave_Sinkula; 09-23-2005 at 04:36 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Thanks. I made two pretty large errors.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Now, that I have that working, I have another questoin.
    All of my functions about 60 of them work w/ pointers. I have matrix inverses, signal processing functions, etc. All of them work w/ pointers since they are the cleanest and most flexible.

    If I know the size of my matrix, I declare it this way.

    Code:
    int A[4][2] = {
    {-1,2},
    {3,4},
    {-5,-5},
    {0,8}
    };
    ;
    I cant use A for my function calls, because my functions require pointers.

    If I used

    Code:
    int** A ;
    I can easily pass A to my functions.

    The way i am working around this is I am just dynamically allocating all of my matrices.

    My question is, is there a way to allocate space during compile time, and initialize the matrix, while at the same time, associating a pointer w/ it? How do you point a float** to a 2-D array?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are a couple of ways

    1. define your array 1 row at a time
    Code:
    int row0 = {-1,2};
    int row1 = {3,4};
    int row2 = {-5,-5};
    int row3 = {0,8};
    int *A[4] = { row0, row1, row2, row3 };
    foo ( A );  // a function accepting int ** parameters
    2. Create an array of pointers
    Code:
    int A[4][2] = {
      {-1,2},
      {3,4},
      {-5,-5},
      {0,8}
    };
    int *pA[4];
    for ( i = 0 ; i < 4 ; i++ ) pa[i] = A[i];
    foo ( pa );
    Or maybe
    http://gcc.gnu.org/onlinedocs/gcc-4....pound-Literals
    Which is either a GNU extension or C99, depending on what you do

    It would look something like this (I think)
    Code:
    int *pA[4] = {
      (int []){-1,2},
      (int []){3,4},
      (int []){-5,-5},
      (int []){0,8}
    };
    foo ( pa );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error handling in recursive function.
    By broli86 in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 02:11 PM
  2. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  3. passing struct to pthread_create
    By rotis23 in forum Linux Programming
    Replies: 4
    Last Post: 05-11-2004, 04:31 AM