12,295,457 members (56,239 online)
Rate this:
See more:
how do i do matrix multiplication involving pointer to arrays?

```void tom::matrixmultiply(void* btr)
{
short* block = (short *)btr;```
`int c[4][4]={1,1,1,1,2,1,-1,-2,1,-1,-1,1,1,-2,-2,-1};`
block is a 4x4 matrix and i want it to be multiplied by matrix c
Posted 7-Mar-11 4:09am
Edited 7-Mar-11 4:24am
v2

Rate this:

## Solution 1

Your casting is not valid. Casting `short*` to `void*`, and `void*` back to `short*` is OK. But what you have is a 2-dimensions array: `short*[]`.

I suggest that you declare your matrix like that:
```short c[4*4] = ...
```

If you want to use 2 dimensionnal arrays, you should initialize like that:
```short c[4][4] = {
//first row
{ 1, 1, 1 },
//second row
{ 2, 1, -1, -2},
//third row
{ 1, -1, -1, 1 },
//fourth row
{ 1, -2, -2, -1 } };```

If you want to use this fixed size array in a function, you can use the fixed size in the parameter list:
```void yourFunction(short mat[4][4])
{
...
}
//or:
void yourFunction2(short (*mat)[4])
{
...
}
```

But if you want to use `short*`, then you need to work with 1 dimension array:

```short* mat = new short[rows * columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
{
mat[i * columns + j] = ...;
}
...
delete [] mat;
```

With 1 dimension array, you need to keep somewhere the size of your matrix:
```void yourFunction(short* mat, int rows, int columns)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
{
mat[i * columns + j] = ...;
}
}
```

You can use a `typedef`:
```typedef short (*MATRIX4x4)[4];
void yourFunction(void* ptr)
{
MATRIX4x4 mat = (MATRIX4x4)ptr;
//do whatevet you want
mat[0][0] = ...;
}
void main()
{
short matrix[4][4] = ...;
yourFunction(matrix);
}
```
But I don't recommand using void pointers...
v6
SMART LUBOBYA 7-Mar-11 10:28am

what iam not sure is how to do c x block with block written in the form short*block =(short*)btr
SMART LUBOBYA 7-Mar-11 10:46am

is there a way of keeping void tom::matrixmultiply(void* btr) the same but changing short*block = (short*)btr into something like short block[4][4]? thanks in advance
Olivier Levrey 7-Mar-11 11:02am

SMART LUBOBYA 8-Mar-11 4:06am

olivier that was really helpful. be blessed forever
Olivier Levrey 8-Mar-11 4:16am

Top Experts
Last 24hrsThis month
 OriginalGriff 595 Richard MacCutchan 435 Karthik Bangalore 319 Peter Leow 258 Richard Deeming 170
 OriginalGriff 8,825 ppolymorphe 5,362 CHill60 5,260 Karthik Bangalore 4,726 Sergey Alexandrovich Kryukov 4,460