Click here to Skip to main content
12,295,457 members (56,239 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
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

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

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] = ...;
    }
}

Answer to your comment Is there way to keep the void*...
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...
  Permalink  
v6
Comments
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
   
See my updated answer.
SMART LUBOBYA 8-Mar-11 4:06am
   
olivier that was really helpful. be blessed forever
Olivier Levrey 8-Mar-11 4:16am
   
Thank you :) I am glad I could help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 7 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100