Re: Structure



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
.



Relevant Pages

  • Re: legality of forward declaration
    ... how do you interpret the following paragraph from the C99 Standard ... Array declarators ... A declaration of a parameter as ??array of type?? ... it is the compiler that is in error. ...
    (comp.lang.c)
  • Re: keyword extern
    ... >> definition or declaration until the end of the translation unit being ... >> One never needs to use the extern keyword with a function definition, ... >> ...then here is how a C compiler understands them. ... If any code in the source file access 'x', ...
    (comp.lang.c)
  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Something wrong in my program
    ... foo.c:27: warning: implicit declaration of function `isspace' ... This lets the compiler help you out by ... The array size below is not a constant expression. ... buffer array "static" (often a bad idea in larger programs, ...
    (comp.lang.c)
  • Re: Question about multiple files
    ... but say I declare with extern. ... > the compiler can differentiate between declaration and definition just ... >> declaration it finds and in this way can I write code expecting it to be ... > When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)