0

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 );
    }
Luis
  • 1,236
  • 5
  • 22
  • 31

2 Answers2

1

The code

typedef struct Elem {
  char * itag;
  char * cont;
  char * etag;
  struct Elem * previous;
} Element;

does not define an Element. It only tells the compiler its structure. Not one element is defined.

Try having

extern Element myElement;

In the header file.

In the corresponding .c file put

Element myElement;

That will reserve space for myElement

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • The main problem is how can initialize Element, declared externally inside a `if` branch and have it accessible in other iterations of the function containing this `if` branch, it this `if` branch or in `else if` branches. – Luis Apr 26 '12 at 13:59
  • `Element` is the data type. `myElement` is the variable that contains the data. So to set `itag` for example do `myElement.itag=...` in your code. – Ed Heal Apr 26 '12 at 14:03
1

You should use the keyword « extern ». For example :

f.h :

#ifndef H_LP_F_20120426154230   
#define H_LP_F_20120426154230 

typedef struct Elem {
    char *itag;
    char *cont;
    char *etag;
    struct Elem *previous;
} Element;

extern Element myStruct;

#endif

f.c

#include "f.h"

Element myStruct;
/* nom I can use myStruct */
md5
  • 23,373
  • 3
  • 44
  • 93