Re: External structs - newbie question



Simon wrote:
I'm having difficulty with a struct in two of my c files.

The header file for csd.c (the main program) contains the typedef for a
struct called tiles. There is only one struct in use (called "map")
throughout the time that the program is run, and as it's used by
virtually all of the functions it's currently a global. I realise that
this is probably bad practice but I'm still pretty far down the curve
when it comes to c.

The file org.c contains some code which alters certain of the values
within "map". I'd like to declare "map" in csd.c and then use extern
to tell org.c that it exists. Unfortunately, nothing that I do seems
to be able to achieve this. I'm definitely including the typedef in
both files, but if I put a statement like "extern map" in org.c and try
and compile it gives an error "subscripted value is neither array nor
pointer" when it gets to the line "map[i][j].terrain = 1;".

The only way that I can get the thing to compile is to have a header
file shared by both csd.c and org.c which contains the typedef followed
by the declaration of the global variable map. This makes me nervous
because as far as I can see the global "map" is being declared twice -
once by each of the files.

Declaring it in a header file is the right way to go. If you put the keyword extern before the declaration, it will not be a problem declaring it twice. Just make sure that you don't put an initialiser on it.

You should declare it extern in the header, which is included in both files. But you should also define it (without extern), in one of the files only.

Below I've given an example layout for your header file. Note each of the things in it: include guards, global constants, type definitions, global variable declarations and function declarations.

/* map.h */

/* Include guards */

#ifndef H_MAP
#define H_MAP

/* Global constants */

#define MAP_HEIGHT 30
#define MAP_WIDTH 30

/* Type definitions */

struct tile
{
int terrain;
};

/* Global variable declarations */

extern struct tile map[MAP_HEIGHT][MAP_WIDTH];

/* Global function declarations */

void init_map(void);

#endif



/* csd.c */

#include "map.h"

/* Define map in only one of the files */

struct tile map[MAP_HEIGHT][MAP_WIDTH];

int main(void)
{
init_map();

return 0;
}



/* org.c */

#include "map.h"

void init_map(void)
{
int i, j;
for(i = 0; i < MAP_HEIGHT; i++)
{
for(j = 0; j < MAP_WIDTH; j++)
{
map[i][j].terrain = 1;
}
}
}



/* Example compile process for gcc */

gcc -ansi -pedantic -Wall -W -O2 -c csd.c
gcc -ansi -pedantic -Wall -W -O2 -c org.c
gcc -ansi -pedantic -Wall -W -O2 csd.o org.o -o csd
../csd

--
Simon.
.



Relevant Pages

  • Re: Setfocus / killfocus on cbutton
    ... Because it is a static struct, declared at the global scope level in this compilation ... syntax change is minor, and the effect is the same. ... build other button tables for other dialogs, you would put this typedef in another header ... declaration as well as if I try to add some class variables ...
    (microsoft.public.vc.mfc)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: If you could change the C or C++ or Java syntax, what would you like different?
    ... which reifies a struct type a "definition", or is that also a declaration? ... I've seen people refer to the one that says what's in the struct as ... mapping of space to interpretation, but to the words used to denote them. ... that acts like typedef except that the new type is distinct from the old ...
    (comp.lang.c)
  • Re: how to define a function pointer variable witout typdef?
    ... > typedef name, the entity is an alias ... > typedef int foo; ... I think that a complete structure declaration: ... struct list_node *next; ...
    (comp.lang.c)
  • Re: Newbie question
    ... >>> What I've done is declare a struct in a header file, ... >> Declaring structs, typedefs, function prototypes etc yes, defining ... >the header file and not again referenced in the code. ... >the header file in each code file is a duplicate declaration, ...
    (comp.lang.c)