如何在C中的动态结构中使用3个以上的箭头操作符? [英] How to use more than 3 arrow operator in dynamic structure in C?

查看:63
本文介绍了如何在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 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 in ab is a pointer to a single struct of type nest. It's not a pointer to an array of structs of type nest. So (name+i) (in the last two for-loops) is invalid -- unlike (p+i), which is valid, because *p is a pointer to an array of structs of type ab.

You simply need to change this

(p+i)->(name+i)->c


to this:

(p+i)->name->c


这篇关于如何在C中的动态结构中使用3个以上的箭头操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆