A
Adam Chapman
Adam said:Thankyou all. Sorry if it seemed like i was ignoring, Im sure you know
how hard it is to take everything in when you are stressed and
confused!#include <stdlib.h>
float *mat_mult(float *matrixA, float *matrixB, int nRows, int nCols)
{
float *resultset;
int row, col;
/* allocate space for the result set, point resultset at the space */
if ((resultset = malloc(nRows * nCols * sizeof(float))) == NULL)
return NULL; /* first error condition */
/* at this point, resultset points at a space big enough for our
entire
results table. Now, let's initialize it */
for (row = 0; row < nRows; ++row)
{
for (col = 0; col < nCols; ++col)
{
*(resultset + (row * nCols) + col) = 0.0;
}
}
/* matrix multiply A and B to result in resultset */
for (row = 0; row < nRows; ++row)
{
for (col = 0; col < nCols; ++col)
{
*(resultset + (row * nCols) + col) =
*(matrixA + (row * nCols) + col) *
*(matrixB + (row * nCols) + col);
}
}
return resultset;
}
#define Arows 3
#define Acols 3
#define Brows 3
#define Bcols 3
float A[Arows][Acols]={ {1,2,3}, {4,5,6}, {7,8,9} };
float B[Brows][Bcols]={ {9,8,7}, {6,5,4}, {3,2,1} };
#include <stdio.h>
int main(void)
{
float *Results;
int row, col;
/* Sanity check our array bounds */
if ((Arows != Brows) || (Acols != Bcols))
{
printf("Gaak - our arrays aren't of the same size\n");
return EXIT_FAILURE;
}
/* try matrix multiplication - exit EXIT_FAILURE on failure */
if ((Results = mat_mult(&A[0][0],&B[0][0],Arows,Acols)) == NULL)
{
printf("Gaak - we ran into an error\n");
return EXIT_FAILURE;
}
/* Print out the result set */
for (row = 0; row < Arows; ++row)
{
printf("[%d]\t",row);
for (col = 0; col < Acols; ++col)
{
printf("%f\t",*(Results + (row * Acols) + col));
}
printf("*\n");
}
printf("End of results\n");
/* Free the allocated space */
free(Results);
return EXIT_SUCCESS;
}When I try to compile this, i get " error C2440: '=' : cannot convert
from 'void *' to 'float *' " from the lineThe error is surely coming from thepart. My understanding is that before that particular line, resultset
is a pointer to a memory block big enough to hold a float.
The above line tries to make resultset a pointer to a larger memory
block, big enough for the array we are making.
I can't see what would be seen as " void * " in that expression
though. Could it be that malloc returns a void type (i.e. nothing?)
malloc returns a void * which in C should convert to (alost) everything
automagically. It does not in C++ (it requres a cast, so it seems likely
you're operating your compiler in C++ mode.
Bye, Jojo- Hide quoted text -
- Show quoted text -
that looks very likely, as I am using Visual C++ for the first time! I
changed the file name extension to .c rather than .cpp but apparently
that's not enough.