如何在C中的动态结构中使用3个以上的箭头操作符? [英] How to use more than 3 arrow operator in dynamic structure in C?
本文介绍了如何在C中的动态结构中使用3个以上的箭头操作符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int main()
{
struct nest
{
int c,d;
};
struct ab
{
int a, b;
struct nest * name;
};
struct ab * p;
p = calloc( 10 , sizeof ( struct ab));
int x1;
for (x1 = 0 ; x1< 10; x1 ++)
(p + x1) - > name = malloc( sizeof ( struct nest));
int i;
for (i = 0 ; i< 10; i ++)
{
(p + i) - >(名称+ i) - > c = i + 10;
}
for (i = 0 ; i< 10; i ++)
{
printf( %d \ n,(p + i ) - >(名+ I) - &℃);
}
return 0 ;
}
我的尝试:
此代码显示错误:'('for token in for loops
解决方案
* name $}之前的预期标识符c $ c> in
类型结构的数组的指针。所以ab
是指向nest
类型的单个结构的指针。它是不指向 c(name + i)
(最后两个for
-loops)无效 - 与(p + i)
不同,这是有效,因为* p
是一个指向数组类型结构的指针ab
。
您只需更改此
(p + i) - >(姓名+ i) - > c
到此:
(p + i ) - >名称 - &以及c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
struct nest
{
int c,d;
};
struct ab
{
int a,b;
struct nest *name;
};
struct ab *p;
p=calloc(10,sizeof(struct ab));
int x1;
for(x1=0;x1<10;x1++)
(p+x1)->name=malloc(sizeof(struct nest));
int i;
for(i=0;i<10;i++)
{
(p+i)->(name+i)->c=i+10;
}
for(i=0;i<10;i++)
{
printf("%d\n",(p+i)->(name+i)->c);
}
return 0;
}
What I have tried:
This code shows an error:expected identifier before '(' token inside for loops
解决方案
*name
inab
is a pointer to a single struct of typenest
. It's not a pointer to an array of structs of typenest
. So(name+i)
(in the last twofor
-loops) is invalid -- unlike(p+i)
, which is valid, because*p
is a pointer to an array of structs of typeab
.
You simply need to change this
(p+i)->(name+i)->c
to this:
(p+i)->name->c
这篇关于如何在C中的动态结构中使用3个以上的箭头操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文