[英]C typedef: pointer to struct
For my personal entertainment I am learning C. I am trying to write a matrix calculation header. 为了个人娱乐,我正在学习C。我正在尝试编写矩阵计算标头。 I have a function to show the matrix, rmat_show(rmatrix *r), which should printf the entire matrix.
我有一个函数来显示矩阵rmat_show(rmatrix * r),该函数应该打印整个矩阵。 However, it seems that my data type rmatrix is not passed well to this function.
但是,似乎我的数据类型rmatrix无法很好地传递给此函数。
rmat.h: 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. 在rmat_show之后,我尝试使用另一个函数杀死te矩阵。 It yields the same error, which is: 'incompatible type for argument 1 of 'rmat_show' expected 'rmatrix' but argument was of type 'struct rmatrix *''.
它会产生相同的错误,即:'rmat_show'的参数1的类型不兼容,预期为'rmatrix',但参数的类型为'struct rmatrix *'。 I have tried searching 'pointer to typedef' and similar terms, without result.
我尝试搜索“ typedef指针”和类似术语,但没有结果。 I believe the typedef declaration is not carried over to the function defenition.
我相信typedef声明不会保留到函数定义中。 I use msys and mingw on windows 7.
我在Windows 7上使用msys和mingw。
Thanks for the help. 谢谢您的帮助。
Edit: added the typedef line I miscopied. 编辑:添加了我误复印的typedef行。
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 您可能还想使用结构的正确名称
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++ 是否包含struct取决于您的编译器版本C / C ++
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: 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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.