file: element.h
#ifndef ELEMENT_H
#define ELEMENT_H
typedef struct Elem {
char * itag;
char * cont;
char * etag;
struct Elem * previous;
} Element;
void printElement ( Element * );
#endif /* ELEMENT_H */
I declared and defined a structure Element
in element.h and element.c (not shown but where malloc is performed).
One of the functions of file parser.c
should access Element
.
If some condition apply, a new pointer to Element
is created, one of the pointer attributes is filled up.
Some iterations afterwards, if some other condition applies, another pointer attribute gets some text.
Then, when some other condition is met, the pointer to Element
should be passed to a function of another file: output.c
.
My question is: how and where should I call Element
.
If create the pointer to it inside an if
condition it's an automatic variable there. Not visible when the function is iterated.
I can declare it static
, but the compiler returns an error error: 'e' undeclared (first use in this function)
. Ex: in iteration 1, the pointer is created in one branch of the if
statement; in iteration 2, another branch of if
is accessed and I do something like e->etag = "a";
If I declare extern Element * e;
, get the same error in the second branch of the if
(first else if
).
file output.c
#include element.h
write_element ( Element * e )
{
write_to_disk(... e->itag, e->etag);
}
file parser.c
#include "element.h"
# some other functions
void parser ( char * f, char * b )
{
if ( something ) {
/* Need to access externally defined Element type structure, but it should be visible in all `parser` function */
Element * e;
e->itag = ... realloc(...)
...
} else if (..... ) {
/* Should assume a pointer to Element is already created */
e->etag = "a";
} else if ( .... ) {
/* Should assume a pointer to Element is already created */
/* and itag, etag and cont have some text */
write_element( e );
}