Structure

B

balasam

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();
struct stu_dbase {
char name[50];
int reg_no;
}stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};
main()
{
fun();
}

Second file : fun.c

extern stu[];
void fun()
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
}

Regards,

Balasam.KM
 
M

Murali

How do you compile the program?

$ cc -c fun.c
$ cc main.c fun.o

or something like it should be done.

- Murali
 
V

Vladimir Oka

balasam opined:
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.

What error message?
Program :

First file : main.c

void fun();
struct stu_dbase {
char name[50];
int reg_no;
}stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};
main()
{
fun();
}

Second file : fun.c

extern stu[];

This looks like a god place for one. You probably want:

extern struct stu_dbase stu[];
void fun()
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
}

Regards,

Balasam.KM


--
Oh, that sound of male ego. You travel halfway across the galaxy and
it's still the same song.
-- Eve McHuron, "Mudd's Women", stardate 1330.1

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
B

balasam

Vladimir Oka wrote:

Second file : fun.c

extern stu[];

This looks like a god place for one. You probably want:
extern struct stu_dbase stu[];

> void fun()
> {
> int i;
> for(i=0;i<3;i++)
> printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
> }

I also checked this , but it is also not working.Its displays error
message.

Thanks for your reply

Regards,

Balasam.MK
 
V

Vladimir Oka

balasam opined:
Vladimir Oka wrote:

Second file : fun.c

extern stu[];

This looks like a god place for one. You probably want:
extern struct stu_dbase stu[];

void fun()
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
}

I also checked this , but it is also not working.Its displays error
message.


Again: what exact error message?

You also do not seem to specify a format specifier for `stu.name`.
If memory serves, an "%s" would be about right. Your compiler may be
smart enough to realise that the format string does not match the
argument list (or vice versa).

--
We have found all life forms in the galaxy are capable of superior
development.
-- Kirk, "The Gamesters of Triskelion", stardate 3211.7

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
S

stathis gotsis

balasam said:
Dear friend,
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();
struct stu_dbase {
char name[50];
int reg_no;
}stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};
main()
{
fun();
}

Second file : fun.c

extern stu[];
void fun()
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
}


Hello there,
why don't you try something like the following:

main.c:

#include "fun.h"

struct stu_dbase stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};

int main()
{
fun();
return 0;
}


fun.h:

struct stu_dbase {
char name[50];
int reg_no;
};

extern struct stu_dbase stu[];

void fun();


fun.c:

#include <stdio.h>
#include "fun.h"

void fun()
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName:%s\n",stu.reg_no,stu.name);
}
 
B

Barry Schwarz

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.

int main(void) if you please.

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.reg_no,stu.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
 
D

Dave Thompson

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.
Not quite. The old-style form declares the function to take
_unspecified_ argument types, but they must be compatible with an
old-style definition: fixed in number (not variadic) and of types
after the default promotions i.e. not char, short, or float. All
invocations should be consistent, but the compiler is not required to
check; if it does, it will probably start with the first.
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.
Right. Nit: except officially declarations in C are are syntactically
separate from statements. In C89/90 the contents of a block, including
a function body, is zero or more declarations followed by zero or more
statements. In C99, where declarations can be interleaved with
statements, the contents are zero or more block-item's each of which
is either a declaration or a statement.

In C++, which allowed interleaving from the start, statements
syntactically include declarations, and block contents is zero or more
statements. In Fortran declarations are a form of statement, but there
are restrictions on the ordering of statement types which require all
declarations in a subprogram to precede all executable statements. In
PL/I declarations are a form of statement and may be anywhere -- even
after uses of the entities they declare.
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.
Because it is at file scope therefore it has static duration. In
general objects declared at file-scope can have either internal or
external linkage; this is external because internal was not specified
with 'static' either here or previously.
However, the type is "known" only in the compilation unit
(source module) in which it appears.
Right.

{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.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.

Not UB -- excess arguments to any variadic function including printf
are ignored; as long as the evaluation in/preceding the function call
is well-defined, as this is, there is no UB. It is usually including
in this case wrong = not what was wanted, but not UB.


- David.Thompson1 at worldnet.att.net
 

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,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top