[英]Syntax error when using pointers through function calling and parameters in C
I am getting a syntax error when trying to allocate space for a multidimensional matrix.尝试为多维矩阵分配空间时出现语法错误。 I am new to coding in C, so anything will help.我是 C 编码的新手,所以任何事情都会有所帮助。 The error occurs when trying to access the matrix structure elements in the read_matrix function.尝试访问 read_matrix 函数中的矩阵结构元素时发生错误。 The syntax error is "expression must have struct or union type".语法错误是“表达式必须具有结构或联合类型”。 Where the error is produced is commented out in the read_matrix function.产生错误的地方在 read_matrix 函数中被注释掉。
typedef struct {
int *elements;
int rows;
int columns;
} matrix;
void main() {
matrix a, b, c;
void read_matrix(matrix *);
void deallocate(matrix *);
void print(matrix);
matrix add(matrix, matrix);
matrix subtract(matrix, matrix);
matrix multiply(matrix, matrix);
read_matrix(&a);
read_matrix(&b);
c = add(a, b);
deallocate(&c);
c = subtract(a, b);
deallocate(&c);
c = multiply(a, b);
}
void read_matrix(matrix *z) {
int d1, d2, i, x, y, val;
printf("What is the first dimension of the array? ");
scanf("%d", &d1);
printf("What is the second dimension of the array? ");
scanf("%d", &d2);
*z.elements = (int *)calloc(d2, sizeof(int));
*z.rows = d1;
*z.columns = d2;
/* error here. It isn't letting me access the
elements/rows/columns of the matrix */
/* additional code below here */
}
Operator .
操作员.
has higher precedence than unary operator *
, which means that if you want to access struct members through a pointer z
using *
-and- .
具有比一元运算符*
更高的优先级,这意味着如果您想使用*
-and- 通过指针z
访问结构成员.
combination, you have to use parentheses.组合,你必须使用括号。 It your case it should be你的情况应该是
(*z).elements = ...
Alternatively you can use ->
operator或者,您可以使用->
运算符
z->elements = ...
And it is supposed to be int main()
, not void main()
它应该是int main()
,而不是void main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.