Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Posted
Updated 27-Mar-16 11:28am
v3
Comments
Patrice T 27-Mar-16 17:26pm    
Can you explain your arrow problem ?

1 solution

*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
C++
(p+i)->(name+i)->c

to this:
C++
(p+i)->name->c
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Mar-16 21:22pm    
5ed.
—SA
Sascha Lefèvre 28-Mar-16 4:00am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900