Re: Structure
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 29 Apr 2006 17:32:25 -0700
On 28 Apr 2006 20:48:43 -0700, "balasam" <bkmbala@xxxxxxxxx> wrote:
Dear friend,
I am having Linux platform.I am declaring the array of
structure in one file and how can access the values of the array of
structure in another file.
Using " extern " ,to extern the structure but when i
access the values of the array of structure ,the compilers displays the
error message.
Program :
First file : main.c
void fun();
If fun does not take any arguments, say so:
void fun(void);
What you have says fun may take any arguments. The first invocation
should be used to determine what they are.
struct stu_dbase {
char name[50];
int reg_no;
}stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};
This "statement" serves two purposes. It declares the type struct
stu_dbase AND it defines an object of this type. The object is named
stu and happens to be an array of three struct.
The object is defined outside of any function and therefore
has external linkage and static duration. This is frequently referred
to as a global object.
However, the type is "known" only in the compilation unit
(source module) in which it appears.
main()
int main(void) if you please.
{
fun();
Unless you have a C99 compiler, you need a return statement here. Even
if you have a C99 compiler, using the statement allows those of us who
don't to compile your code.
}
Second file : fun.c
extern stu[];
This is a syntax error. You probably meant
extern struct stu_dbase stu[];
But even that has a problem because this is a different source module
and the compiler does not know what type struct stu_dbase is. You
have two options:
You could repeat the declaration of the type from main.c here.
You could place the declaration in a header file and include
that header file in both source modules. This is recommended because
it insures consistency. If you do this, you could include the
external declaration in the header file also (a convenience when many
source modules will need to refer to the global object).
void fun()
void fun(void) please.
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu[i].reg_no,stu[i].name);
You have two arguments after the format string. Your format string
only has one conversion specification. This invokes undefined
behavior. You probably want a %s before the \n.
}
Remove del for email
.
- References:
- Structure
- From: balasam
- Structure
- Prev by Date: Re: String Pattern Matching algo
- Next by Date: Re: String Pattern Matching algo
- Previous by thread: Re: Structure
- Next by thread: Get number of running processes
- Index(es):
Relevant Pages
|