Array of Structure Declaration

F

Faay

Hi all,

I have an array of structure in file1.c which is defined as follows:
struct abc {
char* file_name;
unsigned int line_number;
} abc_table[TAG_MAX];

I need to use the tag_table in another file , say file2.c , so I write
the declaration in file1.h and include the header file1.h in file2.c :

#ifndef FILE1_H
#define FILE1_H

#define TAG_MAX 100
struct abc abc_table[TAG_MAX];

#endif


On compiling , I am getting an error "error C2148: total size of array
must not exceed 0x7fffffff bytes"

The only way I could get across this is to define the structure in the
header file and not in the c file. But then I don't want file2.c to know
whats inside this structure.How do I solve this problem .


Please help
Thanks
 
E

Eric Sosman

Faay said:
Hi all,

I have an array of structure in file1.c which is defined as follows:
struct abc {
char* file_name;
unsigned int line_number;
} abc_table[TAG_MAX];

I need to use the tag_table in another file , say file2.c , so I write
the declaration in file1.h and include the header file1.h in file2.c :

#ifndef FILE1_H
#define FILE1_H

#define TAG_MAX 100
struct abc abc_table[TAG_MAX];

#endif


On compiling , I am getting an error "error C2148: total size of array
must not exceed 0x7fffffff bytes"

The only way I could get across this is to define the structure in the
header file and not in the c file. But then I don't want file2.c to know
whats inside this structure.How do I solve this problem .

If file2.c doesn't know what the struct looks like, file2.c
cannot make any use of the abc_table array except to take the
address of its first element. That being the case, you can put

extern struct abc_table[];

in the header file. (You would need the `extern' even if file2.c
*did* know what the struct is like.)
 
B

Ben Bacarisse

Faay said:
I have an array of structure in file1.c which is defined as follows:
struct abc {
char* file_name;
unsigned int line_number;
} abc_table[TAG_MAX];

I need to use the tag_table in another file , say file2.c , so I write
the declaration in file1.h and include the header file1.h in file2.c :

#ifndef FILE1_H
#define FILE1_H

#define TAG_MAX 100
struct abc abc_table[TAG_MAX];

#endif

It is not clear what you have in what files. For example, the above
has no information about struct abc so the array declaration is not
valid as it stands.

Make a short example and post the actual files with no re-typing. You
need to show the real code.
On compiling , I am getting an error "error C2148: total size of array
must not exceed 0x7fffffff bytes"

The only way I could get across this is to define the structure in the
header file and not in the c file. But then I don't want file2.c to
know
whats inside this structure.How do I solve this problem .

If one part of a program needs to talk about structures without
knowing what is inside them, it must do so via a pointer. Again, it
is likely that your example will help clarify what you are trying to
do.
 
D

David Thompson

Faay said:
Hi all,

I have an array of structure in file1.c which is defined as follows:
struct abc {
char* file_name;
unsigned int line_number;
} abc_table[TAG_MAX];

I need to use the tag_table in another file , say file2.c , so I write
the declaration in file1.h and include the header file1.h in file2.c :

#ifndef FILE1_H
#define FILE1_H

#define TAG_MAX 100
struct abc abc_table[TAG_MAX];

#endif
Technically that produces multiple (tentative->actual) definitions
(i.e. allocations) of abc_table, one in each file.c that #include's
it, and that produces Undefined Behavior, which the implementation
isn't required to detect or diagnose. If as here the definitions are
all identical, in practice most implementations either treat them as a
single definition (i.e. safely coalesce) or give an error.
That struct and TAG_MAX should not want more than 1600 = 0x640 bytes
on any nonabsurd implementation, and exactly 800 = 0x320 bytes on the
the implementation that give the error message shown. I suspect the
actual problem is with some other code of which this was intended to
be a simplified example. And simplified examples are often useful, and
usually preferable. But if a/the problem is actually in the complexity
and/or size of the original, simplifying isn't helpful.
The only way I could get across this is to define the structure in the
header file and not in the c file. But then I don't want file2.c to know
whats inside this structure.How do I solve this problem .

If file2.c doesn't know what the struct looks like, file2.c
cannot make any use of the abc_table array except to take the
address of its first element. That being the case, you can put

extern struct abc_table[];
YM ... struct abc abc_table ... .
in the header file. (You would need the `extern' even if file2.c
*did* know what the struct is like.)

Right. But to keep the structure 'hidden', put/keep the 'struct abc {
.... }' with the *definition* of abc_table (without 'extern' or with
initializer or both) in file1.c
 
N

Nick Keighley

Faay wrote:
I have an array of structure in file1.c which is defined as follows:
struct abc {
    char* file_name;
    unsigned int line_number;
} abc_table[TAG_MAX];

I wouldn't use one monster definition (but opinions vary)

#ifndef FILE1_H
#define FILE1_H

#define TAG_MAX 100

typedef struct
{
char* file_name;
unsigned int line_number;
} abc;

extern struct abc abc_table[TAG_MAX];

#endif

file2.c would look like

#include "file1.h"

struct abc [TAG_MAX];

#ifndef FILE1_H
#define FILE1_H
#define TAG_MAX 100
struct abc abc_table[TAG_MAX];
#endif

that's not possible without using pointers (or something even more
devious). The compiler needs to know how big the array elents are when
it compiles file2.c
     If file2.c doesn't know what the struct looks like, file2.c
cannot make any use of the abc_table array except to take the
address of its first element.  That being the case, you can put
   extern struct abc_table[];

YM ... struct abc abc_table ... .

why? I'm not sure what the ellipses are meant to indicate. Are you
saying the [] should be omitted?
Right. But to keep the structure 'hidden', put/keep the 'struct abc {
... }' with the *definition* of abc_table (without 'extern' or with
initializer or both) in file1.c

I'm not following, you how can you keep the struct hidden?
 
D

David Thompson

On Sun, 15 Nov 2009 13:47:09 -0500, Eric Sosman
     If file2.c doesn't know what the struct looks like, file2.c
cannot make any use of the abc_table array except to take the
address of its first element.  That being the case, you can put
   extern struct abc_table[];

YM ... struct abc abc_table ... .

why? I'm not sure what the ellipses are meant to indicate. Are you
saying the [] should be omitted?
No. I'm saying you need a tag abc before the identifier abc_table.
The ellipsized(?!) parts are left unchanged, so the result is
extern struct abc abc_table [] ; /* spacing added */
I'm not following, you how can you keep the struct hidden?

To be clear, what was requested and offered is to keep hidden 'what's
inside this structure' aka 'what the struct looks like'. If you have

blah.h
extern struct abc abc_table [] ;
/* or with the bound if you like, for information only */
#define NUM_ABC 42 /* or enum */
extern struct abc abc_table [ NUM_ABC ];

blah.c
#include "blah.h"
struct abc { int this; char * that; etc. etc. };
struct abc abc_table [ NUM_ABC ];
/* can combine those in one declaration if you prefer */
.... routines that manipulate struct abc and/or abc_table ...

then the routines in blah.c know all about struct abc and abc_table.

Other translation units that #include "blah.c" know abc_table exists,
and is a structure, but nothing else about it. As Eric said, the only
thing they can do is get the address of the first element (by decay of
the name abc_table) and pass it around, presumably to the routines in
blah.c that can do something useful with it.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top