C语言 指向结构内结构的指针;如何访问没有错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16487861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointer to struct inside a struct; How to access without errors?
提问by Lamberto Basti
As far as I know, if the item inside a structure is a pointer, you call it with ->, if it's a normal value it is used ..
据我所知,如果结构内的项是指针,则使用 调用它->,如果它是正常值,则使用.。
here my typedefs:
这是我的 typedef:
typedef struct
{
char name[50];
int quantity;
}ing;
typedef struct
{
char name[50];
ing *n;
int price, n_ing, max_producity;
}prod;
then I declare a prod a;and I allocate a->n=malloc(n*sizeof(ing));.
But when I try to access to a.n->nameit gives me error.
然后我声明 aprod a;并分配a->n=malloc(n*sizeof(ing));. 但是当我尝试访问a.n->name它时会出错。
All the 4 combinations gives me error "error: invalid type argument of '->' (have 'prod' (or 'ing'))" or "error: subscripted value is neither array nor pointer nor vector".
所有 4 种组合都给我错误“错误:'->' 的无效类型参数(有 'prod'(或 'ing'))”或“错误:下标值既不是数组也不是指针也不是向量”。
Why?
为什么?
int ret_max_producity(prod a, ing n, int dim_n)
{
int max_prod=32100, i;
for(i=0; i<a.n_ing; i++)
{
/* here!->*/if((n[find_ing(n, a.n[i]->name, dim_n)].quantity)/(a.n->quantity)<max_prod)
{
/* here!->*/ max_prod=(n[find_ing(n ,a.n[i]->name, dim_n)].quantity/a.n->quantity);
}
}
if(max_prod==32100)
{
printf("ERROR WHILE FINDING MAX PRODUCITY FOR PRODUCT %s, ABORTING", a.name);
system("pause");
exit(EXIT_FAILURE);
}
return max_prod;
}
int find_ing(ing v, char *s, int dim)
{
int i;
for(i=0; i<dim; i++)
{
if(strcmp(s, v.name)==0)
{
return i;
}
}
printf("\n\nERROR WHILE FINDING INGREDIENT %s IN VECTOR, ABORTING...", s);
system("pause");
exit(EXIT_FAILURE);
}


回答by Magn3s1um
If you declare a prod a, and not a prod * a, then you need to access the members by a.n. Since n is a pointer, it should be a.n->name.
如果你声明的是prod a,而不是prod * a,那么你需要通过an来访问成员,因为n是一个指针,它应该是an->name。
-> is only used if you have a pointer to a structure. . is used when you have an instance of a structure.
-> 仅在您有指向结构的指针时使用。. 当你有一个结构的实例时使用。
prod a;
生产一个;
a.n = malloc(sizeof(ing *)); //Why are you multiplying this number by n? don't do that. a.n = address of instance of ing that you made. a.n = &ing structure.
an = malloc(sizeof(ing *)); //你为什么要把这个数乘以n?不要那样做。an = 您创建的 ing 实例的地址。一个 = &ing 结构。
a.n->whatever = Now you can access This value
an->whatever = 现在您可以访问此值
Edit:
编辑:
Get rid of the type defs. It should read
摆脱类型定义。它应该读
struct Foo{
members
};
struct Foo2{
members
};
Then it should be fine. You're using typedef in the wrong way I believe.
那么应该没问题。我认为您以错误的方式使用 typedef。
struct ing
{
char name[50];
int quantity;
};
struct prod
{
char name[50];
ing *n;
int price, n_ing, max_producity;
};
You also need to pass int he addresses of both of those structs, and then get a pointer out of them. So your function declaration should read:
您还需要传递这两个结构的 int 地址,然后从中获取一个指针。所以你的函数声明应该是:
int ret_max_producity(prod * a, ing *n, int dim_n)
and when calling ret_max_productivity, it shoudl have:
当调用 ret_max_productivity 时,它应该有:
ret_max_productivity(&a, &n, int dim_n);
回答by Mike
As far as I know, if the item inside a structure is a pointer, you call it with ->, if it's a normal value it is used ..
As far as I know, if the item inside a structure is a pointer, you call it with ->, if it's a normal value it is used ..
No, the .operator is used to access the members of a structure, the ->operator is a short cut for dereference and ., so you use that if you have a pointer to your structure. So:
不,.运算符用于访问结构的成员,->运算符是取消引用 and 的捷径.,因此如果您有指向结构的指针,则可以使用它。所以:
ing *n = malloc(sizeof(ing));
n->quantity = 5; // This is the same as
(*n).quantity = 5; // this.
In the most basic sense, to access a structure's members you do this:
在最基本的意义上,要访问结构的成员,您可以这样做:
ing n;
n.quanity = 5;
To access the structure's members if you declare a pointer to your structure, you do this:
如果声明指向结构的指针,要访问结构的成员,请执行以下操作:
ing *n;
n = malloc(sizeof(ing));
n->quanity = 5;
"prod a; and I allocate a->n=malloc(n*sizeof(ing));. But when I try to access to a->n->name it gives me error"
"prod a; and I allocate a->n=malloc(n*sizeof(ing));. But when I try to access to a->n->name it gives me error"
If you are declaring a structure like that, then the reason it's giving you an error is because you're trying to deference something that's not a pointer.
如果你声明了一个这样的结构,那么它给你一个错误的原因是因为你试图尊重不是指针的东西。
a.n->name
would be correct.
会是正确的。
EDIT
After seeing your code I see what your problem is:
编辑
看到你的代码后,我明白你的问题是什么:
* here!->*/if((n[find_ing(n, a.n[i]->name, dim_n)].quantity)/(a.n->quantity)<max_prod)
The issue is you're doing more dereferencing than you think. The [i]is doing an add and dereference then the ->is doing another level of it.
问题是您执行的取消引用比您想象的要多。该[i]做的加载和间接引用,则->是做它的另一个层面。
Here's a quick example of how it should look:
这是它应该如何显示的一个快速示例:
prod a;
a.n = malloc(3 * sizeof(ing)); // array of 3 ing structs
a.n[1].quantity = 5; // access quanity member of ing struct 1
EDIT 2
Here's the line in question:
编辑 2
这是有问题的行:
* here!->*/if((n[find_ing(n, a.n[i]->name, dim_n)].quantity)/(a.n->quantity)<max_prod)
Here's what it should have been:
这应该是这样的:
if((n[find_ing(n, a.n[i].name, dim_n)].quantity)/(a.n->quantity)<max_prod)
EDIT 3
Totally overlooked the obvious. You're passing a single ingstruct to this function called n:
编辑 3
完全忽略了显而易见的事情。您将单个ing结构传递给这个名为的函数n:
int ret_max_producity(prod a, ing n, int dim_n)
^
|
that's just a single struct totally different than
the n that's passed in as a member in the `a` struct
But then in your if check you're trying to access this struct as an array:
但是在您的 if 检查中,您正尝试将此结构作为数组访问:
n[find_ing(n, a.n[i]->name, dim_n)].quantity
You do nothave an array of ingstructures in the nvariable, you only have a single ingvariable. So it's yelling at you for treating this single struct as an array of structs.
您在变量中没有ing结构数组n,您只有一个ing变量。所以它对你大喊大叫,因为你把这个单一的结构视为一个结构数组。

