struct struct

  • Follow


i have these structures in my program,

typedef struct Loading {
	char **name;			  // Name of the program
	int pid; 		               // current pid of the program
	struct Loading *prec;
	struct new_phrase *line;
} Loading ;

typedef struct {
	int N;				       // number of words 
	char **T;			    // words stored in an array
} new_phrase;

i want to initialize N through line in the structure Loading.
i dont know how to do that. 
i thought of something like (*load).(*line).N=45;
while having 
Loading *load;

any suggestions?

0
Reply chitlesh1 (22) 6/12/2005 2:51:55 PM

Clunixchit wrote:

> i have these structures in my program,
> 
> typedef struct Loading {
> 	char **name;			  // Name of the program
> 	int pid; 		               // current pid of the program
> 	struct Loading *prec;
> 	struct new_phrase *line;
> } Loading ;
> 
> typedef struct {
> 	int N;				       // number of words 
> 	char **T;			    // words stored in an array
> } new_phrase;
> 
> i want to initialize N through line in the structure Loading.
> i dont know how to do that. 
> i thought of something like (*load).(*line).N=45;
> while having 
> Loading *load;
> 
> any suggestions?

     The `line' element of your `struct Loading' is a pointer
to a `struct new_phrase', but the compiler does not know what
a `struct new_phrase' contains -- in particular, it does not
know whether it contains an element named `N', nor what type
that element might be.  This is because you have not declared
the `struct new_phrase' type anywhere.

     Look carefully at the second declaration: it declares an
"anonymous" struct type and declares `new_phrase' as an alias
for that anonymous type.  Either change the second declaration
to look like the first (so `struct new_phrase' is known), or
else change the type of `line' from `struct new_phrase*' to
`new_phrase*' (and put the second declaration first so that
`new_phrase' is declared before it's used.)

     Once you've done this, rewrite the assignment statement
as `load->line->N = 45;'.  `(*(*load).line).N = 45;' would
also work, but is clumsier.

-- 
Eric Sosman
esosman@acm-dot-org.invalid

0
Reply esosman (1335) 6/12/2005 3:30:50 PM


> Eric Sosmanwrote:
Clunixchit wrote:
> 
> Either change the second declaration
> to look like the first (so `struct new_phrase' is known), or
> else change the type of `line' from `struct new_phrase*' to
> `new_phrase*' (and put the second declaration first so that
> `new_phrase' is declared before it's used.)
> 
>      Once you've done this, rewrite the assignment statement
> as `load->line->N = 45;'.  `(*(*load).line).N = 45;' would
> also work, but is clumsier.
> 

im using 3 files for my program.
first is the main function where i wrote load->line->N = 45;
in the second, there is the new_phase structure
the last one contains the Loading structure
hence how can i declare new_phrase before the Loading structure ?
my main:

 	new_phrase phrase;
 	Loading *Load = NULL;

0
Reply chitlesh1 (22) 6/12/2005 4:07:12 PM

Clunixchit wrote on 12/06/05 :
> i have these structures in my program,
<...>

Stay simple. define things before using them :

typedef struct
{
   /* number of words */
   int N;

   /* words stored in an array */
   char **T;
}
new_phrase;

typedef struct Loading
{
   /* Name of the program */
   char **name;

   /* current pid of the program */
   int pid;

   struct Loading *prec;
   new_phrase *line;
}
Loading;

int main (void)
{
   Loading x =
   {0, 0, 0, 0};
   new_phrase y =
   {0, 0};

   x.line = &y;

   x.line->N = 123;

   return 0;
}

-- 
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

0
Reply emdel (952) 6/12/2005 6:19:58 PM

3 Replies
38 Views

(page loaded in 0.06 seconds)

Similiar Articles:
















6/22/2012 2:19:27 AM


Reply: