# Thread: double ***x_matrix

1. ## 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. 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]

3. 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. 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

5. 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. (*x_matrix)[0][0] = alldoubles[z];

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

and then
temp_x[0][0] = value;

7. 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