Thread: double ***x_matrix

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    4

    double ***x_matrix

    My instructor has given me a project, specifically stating that we are not allowed to modify function prototypes. The thing is, this double matrix bounces from being a double pointer for allocation, a triple pointer for assigning values, and back to a double pointer for multiplication. (It's for a matrix multiplication thingie)

    As I have it so far, x_matrix[0][0][1] = #, x_matrix[0][1][0] = #, for example. The first [] never changes. The values are assign a'ok, but in the multiplication function I only have access to x_matrix[#][#] , which doesn't do any good.

    double **thecolumn;
    thecolumn = mm_alloc(thesizeofmatrix);
    x_matrix = &thecolumn;

    Is there anyway I can turn x_matrix into a double pointer, seeing as it's first [] is useless?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    double pointer for allocation, a triple pointer for assigning values
    Are you sure it is not other way around?

    Probably - you teacher wants to see something along the lines:
    Code:
    double **matrix = malloc(rows * sizeof *matrix);
    
    if(!matrix)
       return ERROR_OUT_OF_RESOURCES;
    
    
    for(i=0;i<rows;i++)
    {
       matrix[i] = malloc(cols * sizeof(*matrix[i]);
       if(!matrix[i] )
       {
          /* free allocated memory */
          return ERROR_OUT_OF_RESOURCES;
       }
    }
    now you can pass matrix as double** pointer to function
    and access members as matrix[i][j]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    Thank you for the quick response! I'm pretty sure I wasn't mistaken though, here's the function prototypes:

    double ** mm_alloc( int xysize)

    void mm_read (int *xysize, double ***x_matrix, double ***y_matrix)

    double ** mm_multiply (int xysize, double **x, double **y)


    mm_alloc is called from inside of mm_read. The only way I know how to is assign a pointer from x_matrix to the double pointer returned from mm_alloc. But I'm kinda screwed for mm_multiply.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    mm_alloc is called from inside of mm_read.
    it means you pass the pointer to be initialized inside the function

    Code:
    double **x_matrix;
    double **y_matrix;
    mm_read (&xysize, &x_matrix, &y_matrix);
    /*here should be an error check but strangly enough mm_read does not return error code */
    and inside the mm_read

    Code:
    *x_matrix = mm_alloc( *xysize);
    *y_matrix = mm_alloc( *xysize);
    where the *xysize was previously initialized - probably from the file or user input

    But I'm kinda screwed for mm_multiply.
    Show the code - it should be very straightforward
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    I think I've gotten my mm_alloc fixed up now, that way makes a lot of sense. After that in mm_read, I assign values to the matrices from an array of doubles. What is proper syntax for this?

    *x_matrix[0][0] = alldoubles[z];

    gives a segmentation fault

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    (*x_matrix)[0][0] = alldoubles[z];

    but to be easier -
    double** temp_x = *x_matrix;

    and then
    temp_x[0][0] = value;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    That did it! Thank you vart, I can now access x[1][1] easily from inside mm_multiply. This is my first project in C (used to C++), and the prof wants us to get good with pointers for when we go into Assembly. Wish me luck, I'm going to need it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM

Tags for this Thread