[英]“Request for member in something not a structure or union”again
sorry if I ask a question that has been done many times before, but i have found no solution. 抱歉,如果我问一个已经做过很多次的问题,但是我没有找到解决方法。
I have this typedef struct 我有这个typedef结构
typedef struct
{
int matrix[row][col];
}Position;
And i create a variable 我创建一个变量
Position park[weeks];
then i call it in a subroutine 然后我在子程序中调用它
void foo(struct Position *park[weeks]...)
and then 接着
park[s].matrix[i][j]=car[k].id;
and i have the "request for etc etc" error, in this line above. 并且我在上面的这一行中出现“ request for etc etc”错误。 What am I doing wrong? 我究竟做错了什么? Sorry for my english. 对不起我的英语不好。
Change 更改
park[s].matrix[i][j]=car[k].id;
to 至
park[s]->matrix[i][j]=car[k].id;
You've declared parks
as an array of pointers to Position
, so you have to use the operator ->
to access the matrix
member. 您已经将parks
声明为一个指向 Position
的指针的数组,因此必须使用运算符->
来访问matrix
成员。
Alternately, you could write 或者,您可以编写
(*park[s]).matrix[i][j]=car[k].id;
but the ->
operator is a little cleaner. 但是->
运算符会更干净。
just delete the * in you foo. 只需删除foo中的*。 Then everything will be ok 那一切都会好的
void foo( Position *park[weeks]...)
==>
void foo(Position park[weeks]...)
Or 要么
void foo(Position *park,...)
Like this: 像这样:
typedef struct
{
int matrix[2][2];
}Position;
void foo(Position *park)
{
park[1].matrix[1][1]=5;
}
int main()
{
Position park[2];
foo (park);
return 0;
}
I don't understand why typedef struct should put in main, however , if you have to , maybe you can put the function in the main too : 我不明白为什么应该将typedef struct放在main中,但是,如果必须这样做,也许也可以将函数放在main中:
int main()
{
typedef struct
{
int matrix[2][2];
}Position;
void foo(Position *park)
{
Position *park1 = (Position *)park;
park1[1].matrix[1][1]=5;
};
Position park[2];
foo (park);
printf("%d\n",park[1].matrix[1][1]);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.