struct struct

C

Clunixchit

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?
 
E

Eric Sosman

Clunixchit said:
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.
 
C

Clunixchit

Eric Sosmanwrote:
Clunixchit said:
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;
 
E

Emmanuel Delahaye

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++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top