473,545 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about global struct variable as a fuction's parameter

Hello! I am working on dividing a single C file into several files.
Now I encounter a problem about the global variables
and can not find a way to solve it.
All global variables and codes used to be in that single file, that
worked OK. But when I divdie that file into several ones, I
have many "invalid use of undefined type" errors.

The four files are main.c, main.h, readLP.h, and readLP.c.

In readLP.h
I have:

#define MAX_NUMROWS 181010
#define MAX_NUMCOLS 201
#define MAX_NUMNZ 9000000
....

typedef struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} a_polytope, aa_polytope;
....

and in main.h, I have:
....
extern struct Polytope a_polytope, aa_polytope ;

in main.c , if the external variable a_polytope is
used as a function's parameters such as :
status = CPXaddrows (env, lp, 0, num_rows, num_nonzero,
a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg,
a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL);
/* this is a CPLEX callable library function */

I will get "invalid use of undefined type `struct
Polytope'" error in compiling.
It was working OK when all codes were in sigle file without using
keyword "extern".

If I change main.h into:
....
extern struct Polytope a_polytope, aa_polytope ;
struct Polytope *pa_polytope, *paa_polytope ;
....
and also change main.c into
pa_polytope = &a_polytope ;
status = CPXaddrows (env, lp, 0,num_rows, num_nonzero,
pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg,
pa_polytope->rmatind,pa_pol ytope->rmatval,
NULL, NULL);

I will get "dereferenc ing pointer to incomplete type"
four times for each pa_polytope->xxxx above.

Does anyone have a suggestion?

Thank you so much!

Nov 14 '05 #1
5 3282


PCHOME wrote:
Hello! I am working on dividing a single C file into several files.
Now I encounter a problem about the global variables
and can not find a way to solve it.
All global variables and codes used to be in that single file, that
worked OK. But when I divdie that file into several ones, I
have many "invalid use of undefined type" errors.

The four files are main.c, main.h, readLP.h, and readLP.c.

In readLP.h
I have:

#define MAX_NUMROWS 181010
#define MAX_NUMCOLS 201
#define MAX_NUMNZ 9000000
...

typedef struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} a_polytope, aa_polytope;
Are you sure this is your code? It may be, of
course, but if so it doesn't mean what I suspect you
intend it to mean. This declaration says "Here is
what a `struct Polytope' looks like, and here are two
aliases for `struct Polytope' -- that is, any time I
write `a_polytope' or `aa_polytope' it is just as if
I had written out `struct Polytope' in full."

Note that this declaration does *not* declare or
define any variables of the type `struct Polytope' --
it just describes `struct Polytope' and creates two
aliases for it.
...

and in main.h, I have:
...
extern struct Polytope a_polytope, aa_polytope ;
This says "Somewhere else I will define two variables
of type `struct Polytope' with these two names." However,
nothing that you've shown actually defines any such
variables.

Another interesting point (the proximal cause of the
diagnostics you're receiving) is that the description of
what a `struct Polytope' looks like appears only in
"readLP.h". If you include "main.h" in a file that doesn't
also include "readLP.h", that description is not available.
The compiler knows that `struct Polytope' is some kind of
a struct type, but doesn't know anything about the elements
that struct type contains: it doesn't know their names,
their types, their sizes, or their arrangement within the
struct. (You will eventually learn that such "incomplete
types" can be useful in certain circumstances, but I think
you may not be quite ready for that lesson yet.)
in main.c , if the external variable a_polytope is
used as a function's parameters such as :
status = CPXaddrows (env, lp, 0, num_rows, num_nonzero,
a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg,
a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL);
/* this is a CPLEX callable library function */

I will get "invalid use of undefined type `struct
Polytope'" error in compiling.
Right: Without "readLP.h", the compiler is ignorant of
the internals of a `struct Polytope'. From the code as shown
it might deduce that the struct has elements named `rhs' and
`sense' and so on, but it has no idea what these names mean.
It was working OK when all codes were in sigle file without using
keyword "extern".
Presumably because the complete declaration of `struct
Polytope' was in that file, too. (But I suspect that the
`typedef' is a recent and probably incorrect addition.)
If I change main.h into:
...
extern struct Polytope a_polytope, aa_polytope ;
struct Polytope *pa_polytope, *paa_polytope ;
...
and also change main.c into
pa_polytope = &a_polytope ;
status = CPXaddrows (env, lp, 0,num_rows, num_nonzero,
pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg,
pa_polytope->rmatind,pa_pol ytope->rmatval,
NULL, NULL);

I will get "dereferenc ing pointer to incomplete type"
four times for each pa_polytope->xxxx above.
Right: The compiler knows of two `struct Polytope'
variables ("Whatever *that* may be," says the compiler)
named `a_polytope' and `aa_polytope'. It also knows of
to pointers `pa_polytope' and `paa_polytope', defined
locally, that can be made to point to those structs (and
to others of the same type). But it's still without any
information about "the insides" of the struct; loosely
speaking, you've told it how to point "at" the struct as
a whole, but not how to "reach inside."
Does anyone have a suggestion?


Back to the C textbook, I think. There are enough
misunderstandin gs on exhibit here that you're probably
better off re-studying the fundamentals than trying to
blunder ahead in flat-Earthian ignorance -- you wouldn't
want to fall off the Edge, would you?

--
Er*********@sun .com

Nov 14 '05 #2
PCHOME wrote:
Hello! I am working on dividing a single C file into several files.
Now I encounter a problem about the global variables
and can not find a way to solve it.
All global variables and codes used to be in that single file, that
worked OK. But when I divdie that file into several ones, I
have many "invalid use of undefined type" errors.

The four files are main.c, main.h, readLP.h, and readLP.c.

In readLP.h
I have:

#define MAX_NUMROWS 181010
#define MAX_NUMCOLS 201
#define MAX_NUMNZ 9000000
...

typedef struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} a_polytope, aa_polytope;
...

and in main.h, I have:
...
extern struct Polytope a_polytope, aa_polytope ;

in main.c , if the external variable a_polytope is
used as a function's parameters such as :
status = CPXaddrows (env, lp, 0, num_rows, num_nonzero,
a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg,
a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL);
/* this is a CPLEX callable library function */

I will get "invalid use of undefined type `struct
Polytope'" error in compiling.
It was working OK when all codes were in sigle file without using
keyword "extern".

If I change main.h into:
...
extern struct Polytope a_polytope, aa_polytope ;
struct Polytope *pa_polytope, *paa_polytope ;
...
and also change main.c into
pa_polytope = &a_polytope ;
status = CPXaddrows (env, lp, 0,num_rows, num_nonzero,
pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg,
pa_polytope->rmatind,pa_pol ytope->rmatval,
NULL, NULL);

I will get "dereferenc ing pointer to incomplete type"
four times for each pa_polytope->xxxx above.

Does anyone have a suggestion?

Thank you so much!


refer eric's reply to understand what you did wrongly.
now i'll tell you how can you correct this, i mean generally how its
done.

in readLP.h //remove typedef
/*typedef*/ struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} ;
extern struct Polytope a_polytope, aa_polytope;

in readLP.c
#include "readLP.h"
struct Polytope a_polytope, aa_polytope;

in any other file -------->
#include "readLp.h"
// optionally you can declare extern struct Polytope a_polytope,
aa_polytope;
//its your wish as it has already been done when you include readLP.h

rest is fine !!!
------------------
i love C
darius

Nov 14 '05 #3
Eric Sosman wrote:

Presumably because the complete declaration of `struct
Polytope' was in that file, too. (But I suspect that the
`typedef' is a recent and probably incorrect addition.)

Yes. The `typedef' is a recent addition. I did not put it in my
orginalcodes. I added it desperately after dozens of trials.
Does anyone have a suggestion?


Back to the C textbook, I think. There are enough
misunderstandin gs on exhibit here that you're probably
better off re-studying the fundamentals than trying to
blunder ahead in flat-Earthian ignorance -- you wouldn't
want to fall off the Edge, would you?

What C textbook will you suggest? I did look up at least 3
C-textbooks, but none of them mentions clearly about the topics of
"#include" and mutiple files project.

Thanks!

Nov 14 '05 #4

Darius wrote:

extern struct Polytope a_polytope, aa_polytope;

in readLP.c
#include "readLP.h"
struct Polytope a_polytope, aa_polytope;

in any other file -------->
#include "readLp.h"
// optionally you can declare extern struct Polytope a_polytope,
aa_polytope;
//its your wish as it has already been done when you include readLP.h

rest is fine !!!

Do you mean that #include "readLp.h" is necessary in any other file
using struct Polytope a_polytope? but "extern struct Polytope
a_polytope" is optional?
Thanks!
Somehow, I do not know why I used to believe that putting the same '
#include "readLP.h" ' into more than 2 different c files would lead
mutiple definitions of variables. Now I know it is OK to do so.
Is it also OK to put 'int test_i = 0;' into that "readLP.h" and
include it in more than on files?
I might be wrong,but to me the 'int test_i = 0;' is a declaration puls
a definition.
I know it is OK to have 'int test_i;' in it becasue 'int test_i;' is
just a declararion. How about puting 'int test_i = 0' there? The
latter is a definition to me(I might be wrong here).

I looked up several C books before but could not find one clearly
describe topics about mutiple c files project, #incldue and other
preprocessor commands, and so on. Do you happen to know which C book
touch those topics in depth?
Thanks!

Nov 14 '05 #5

PCHOME wrote:
Darius wrote:

extern struct Polytope a_polytope, aa_polytope;

in readLP.c
#include "readLP.h"
struct Polytope a_polytope, aa_polytope;

in any other file -------->
#include "readLp.h"
// optionally you can declare extern struct Polytope a_polytope,
aa_polytope;
//its your wish as it has already been done when you include readLP.h
rest is fine !!! Do you mean that #include "readLp.h" is necessary in any other

file using struct Polytope a_polytope? but "extern struct Polytope
a_polytope" is optional?
yes even this code is valid
int func()
{
extern int a; // for some global variable a;
extern int a;
extern int a;
}
Thanks!
Somehow, I do not know why I used to believe that putting the same ' #include "readLP.h" ' into more than 2 different c files would lead
mutiple definitions of variables. Now I know it is OK to do so.
here is a catch; to save yourself from multiple definations

do this
<------------------------------readLP.h--------------------------------->

#ifndef PCHOME_READLP_H
#define PCHOME_READLP_H

<-----------here is all the code-------------->

#endif

you can use any identifier (a unique string) instead of READLP_H, in
this way
when you include that file first time PCHOME_READLP_H is defined and
next time becoz its already defined, the content is not included again.
you got it?

Is it also OK to put 'int test_i = 0;' into that "readLP.h" and
include it in more than on files?
yes!!!, if you follow the rules above, else you'll get double
defination error
but i don't write any defination in .h files. just declare it using
'extern'
I might be wrong,but to me the 'int test_i = 0;' is a declaration puls a definition.
I know it is OK to have 'int test_i;' in it becasue 'int test_i;' is
just a declararion. How about puting 'int test_i = 0' there? The
latter is a definition to me(I might be wrong here).
I think its clear now.

I looked up several C books before but could not find one clearly
describe topics about mutiple c files project, #incldue and other
preprocessor commands, and so on. Do you happen to know which C book touch those topics in depth?
Thanks!


kernighan & ritchie and C unleashed (rest you'll learn by experience)

---------------
darius

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5174
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits,...
31
2141
by: Andrej Prsa | last post by:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a) {
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK" by Dev C++. Here is:(I want to exchange the value of "a" and "b" with function "swap") void swap(int *pa, int *pb) {
7
3113
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
13
3060
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
1
24158
by: amit | last post by:
Hello Group, Does anybody know how I can have a global variable in an HTML file? for instance, I have a fuction (called aFunction() here) and during a mousedown or up event the function is going to be called. The third passing arugment or parameter is myGlobarVar. How can I make this work? since myGlobalVar is defined in a different...
5
1233
by: Hakusa | last post by:
I have the argument items in my class room. class room: def __init__(self, name, description, items*): I thought I remembered from a tutorial I read once, and I've read so many I feel like an expert of them, that putting a little star* above an item makes it accept the argument as a list. But when I tried this, I got an invalid syntax...
9
2678
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I am confused with the way it is declared and defined. a.h ------- : :
27
2748
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 files: ================ module.h ================ #ifndef __MODULE_HDR_INCLUDED__
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.