0

For my personal entertainment I am learning C. I am trying to write a matrix calculation header. I have a function to show the matrix, rmat_show(rmatrix *r), which should printf the entire matrix. However, it seems that my data type rmatrix is not passed well to this function.

rmat.h:

// Matrix struct, m x n matrix. Pointer *value points to the contents.
struct srmatrix {
    int m,n;
    float *value;
};

typedef struct srmatrix rmatrix;

// Show the matrix
void rmat_show(rmatrix* r) {
    int i, j;
    printf("\nshow\n");
    printf("[%dx%d]\n",r->m,r->n);
    for (i = 0; i < r->m; i++) {
        for (j = 0; j < r->m; j++) {
            printf("%d\t",value[(j-1)*r->m+i-1]);
        }
        printf("\n");
    }
}

And I have this as main file:

#include "rmat.h"

int main(int argc, char **argv){
    float val[] = {0.1};
    rmatrix *r = malloc(sizeof(rmatrix));
    r->n = 1;
    r->m = 1;
    r->value = val;
    rmat_show(r);

    return 0;
}

After rmat_show I attempt to kill te matrix with another function. It yields the same error, which is: 'incompatible type for argument 1 of 'rmat_show' expected 'rmatrix' but argument was of type 'struct rmatrix *''. I have tried searching 'pointer to typedef' and similar terms, without result. I believe the typedef declaration is not carried over to the function defenition. I use msys and mingw on windows 7.

Thanks for the help.

Edit: added the typedef line I miscopied.

  • 4
    I don't even see any typedef in the code you posted. –  Nov 11 '13 at 07:12
  • 7
    **Do not put *code* (i.e. functions) in header files.** Only function *prototypes*, structures, typedefs, etc. go in header files. See [this question](http://stackoverflow.com/questions/2831361/how-can-i-create-c-header-files) for the right way to do it. – Jonathon Reinhart Nov 11 '13 at 07:14
  • I added the typedef line, which I missed with copying. – Rhinoceros256 Nov 11 '13 at 07:51
  • There was another bug in the code, in an unused function. Should have commented it out. – Rhinoceros256 Nov 11 '13 at 10:17

2 Answers2

1

Seems you are using the same loop variable twice

 for (i = 0; i < r->m; i++) {
    for (i = 0; i < r->m; i++)

you probably meant

for (i = 0; i < r->m; i++) {
    for (j = 0; j < r->n; j++)

EDIT:

you may want to use the right name of the struct as well

struct srmatrix

not

rmatrix *r = malloc(sizeof(rmatrix));

but

struct srmatrix *r = malloc(sizeof(struct srmatrix)); 

whether you include struct or not depends on your compiler version C/C++

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • This is right, but it won't solve the compiler error. (I don't blame you for that, since currently it's impossible to tell what exactly causes the compiler error; just sayin'.) –  Nov 11 '13 at 07:19
  • Sorry to bother you again, but what do you mean by "whether you include struct or not depends on your compiler version"? –  Nov 11 '13 at 07:29
  • That's incorrect. You're confusing this behavior with that of C++. –  Nov 11 '13 at 07:33
0

This

struct srmatrix {
  int m,n;
  float *value;
};

... 

void rmat_show(rmatrix* r) 
{ 
  ...

  for (i = 0; i < r->m; i++) 
  {
    for (i = 0; i < r->m; i++) 
    {
      printf("%d\t", value[(n-1)*r->m+m-1]);
    }

    printf("\n");
  }

  ...

should be this

rmat.h:

#ifndef RMAT_H
#define RMAT_H

typedef struct rmatrix_s {
  size_t m, n; /* There is no need to define dimension as signed. */
  float * value;
} rmatrix_t;

void rmat_show(rmatrix_t * r);

#endif

rmat.c:

...
#include "rmat.h"

void rmat_show(rmatrix_t * r) 
{ 
  ...

  for (size_t j = 0; j < r->n; j++) 
  {
    for (size_t i = 0; i < r->m; i++) 
    {
      printf("%d\t", value[j*r->m + i]);
    }

    printf("\n");
  }

The main would then have:

...
#include "rmat.h"

int main(int argc, char **argv)
{
  ...
  rmatrix_t * r = malloc(sizeof(*r));
  ...

To compile this do:

gcc -g -Wall -Wextra -o main main.c rmat.c
alk
  • 69,737
  • 10
  • 105
  • 255
  • I changed the code of the struct and for loop to match this, but I still get the same error, where the function expects rmatrix and the variable is of type struct rmatrix * (which is strange, since it should be struct srmatrix *) – Rhinoceros256 Nov 11 '13 at 08:00
  • @Rhinoceros256: As by my modifications just use `rmatrix_t * r`. I update my code to make this obvious. – alk Nov 11 '13 at 08:33
  • @Rhinoceros256: If you liked this answer you are free to upvote it. – alk Nov 11 '13 at 13:00