Hi Everyone,
I have been writting a program where, i have defined the following structure in a header file :
struct the_box
{
int final_value;
int array[10];
};
I have another header file in which i have defined some functions, the main one being :
phase1(struct the_box obj);
In my main program, i have created an object for struct the_box and i have passed it to phase1.
This is giving me lots of errors, can someone please tell me whats going wrong here? How can i pass a user-defined structure to a function when they are in different header files? I have included all the user-derfined header files in both the main program and in the header file containing the functions.
Thanks in advance for any help
Comments
The answer is to include the header file with the struct in the header file where you have your code that calls for the struct. Indeed, where ever it is that you mention the struct, that code needs to #include the header that has the struct.
Are you using C or C++ ? (understand these are DIFFERENT languages).
Also if the above doesn't fix your problem we will need to see more code.
The header file with the structure is :
struct the_box
{
int final_value;
int array[9];
};
The header file with the function is :
#include "the_box.h"
int phase1(struct the_box obj[9][])
{
int i,j;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
printf("%d",obj[i][j].final_value);
return 0;
}
The main program :
#include "the_box.h"
#include "phases.h"
int main()
{
struct the_box obj[9][9];
/*
some input stuff goes here
*/
phases(obj);
return 0;
}
I hope this code helps. Can you please tell me what could be wrong? I am using a GCC compiler. It gives me an error saying "redefinition of struct the_box".
I also tried using the keyword extern and defined the object of the structure globally, that does not seem to help either
[code]/* h file */
typedef struct the_box
{
int final_value;
int array[9];
} Box;
void phase1(Box obj[][9]);
/* c file */
void phase1(Box obj[][9])
{
int i,j;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d ", obj[i][j].final_value);
}
printf("
");
}
}
/* main */
int main()
{
Box obj[9][9];
phase1(obj);
return 0;
}[/code]
Thanks for all the help. I found out what was wrong in my program.
If i have a structure in a header file like below :
/* header1.h */
struct A
{
int abc;
};
and i have another header file which has a function like so :
/* header2.h */
#include "header1.h"
int phase(struct A obj)
{
obj.A = 34;
return 0;
}
then in my main, i only have to include the header file header2.h as this will invoke header1.h on its own.
If you include header1.h and header2.h in the main, it will give multiple struct definition. Thanks for the help.
Thanks for all the help. I found out what was wrong in my program.
If i have a structure in a header file like below :
/* header1.h */
struct A
{
int abc;
};
and i have another header file which has a function like so :
/* header2.h */
#include "header1.h"
int phase(struct A obj)
{
obj.A = 34;
return 0;
}
then in my main, i only have to include the header file header2.h as this will invoke header1.h on its own.
If you include header1.h and header2.h in the main, it will give multiple struct definition. Thanks for the help.