1

I'm trying to copy values from an array of floats to a 4x4 floats matrix but keep getting this annoying error "incompatible types when assigning to type ‘float[4]’ from type ‘float’" please help me...

This is the typedef:

typedef float mat[4][4];

and then the function goes like this:

void function_Read(mat *matrixPointer, float *arrayOfFloats) {

    int indexOfArray;
    int rowIndexOfMatrix = 0;
    int columnIndexOfMatrix = 0;
    int sizeOfArrayOfFloats = sizeof (arrayOfFloats) / sizeof (arrayOfFloats[0]);

    for (indexOfArray = 0; indexOfArray < sizeOfArrayOfFloats; indexOfArray++) {
        /* the line below causes the error */
        matrixPointer[columnIndexOfMatrix][rowIndexOfMatrix] = arrayOfFloats[indexOfArray];

        columnIndexOfMatrix++;

        if (columnIndexOfMatrix == 4) {
            columnIndexOfMatrix = 0;
            rowIndexOfMatrix++;
        }
        if (rowIndexOfMatrix == 4) {
            rowIndexOfMatrix = 0;
        }
    }
}

Plus, can someone add a link to a document for advanced pointers stuff in c ? I'm always letting the debugger flash errors and then I correct them sometimes without even understanding the problem.

Thanks !

Loves2Develop
  • 774
  • 1
  • 8
  • 29
  • `mat *matrixPointer` is the problem. – Pubby May 08 '13 at 22:24
  • 3
    Since `matrixPointer` is a `mat*`, you should access its elements through `(*matrixPointer)[col][row]` rather than `matrixPointer[col][row]`. – Fabien May 08 '13 at 22:26
  • Also note that `sizeOfArrayOfFloats` doesn't hold the expected value, pointers are not arrays, etc... –  May 08 '13 at 22:27
  • float *ptr= arrayOfFloats; int count=0; while ( *(ptr) != '\0' ) { ptr++; count++; } That could be used to get you the length of the array. – blackmambo May 08 '13 at 22:32
  • 1
    The answer to [this SO question](http://stackoverflow.com/questions/4523497/typedef-fixed-length-array) explains what is going on. – Raymond Chen May 08 '13 at 22:33
  • Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson May 08 '13 at 22:43

0 Answers0