How to access a structure declared in other file using extern keyword in 'c' language
I have 3 files . In one file I declared a structure and in another file which has the main, I am trying to access that structure using extern key word --------
//a.c---
include<stdio.h>
extern struct k ;
extern int c;
int main()
{
extern int a,b;
fun1();
fun2();
c=10;
printf("%d\n",c);
struct k j;
j.id=89;
j.m=43;
printf("\n%d\t%f",j.id,j.m);
}
//1.c
#include<stdio.h>
struct k
{
int id;
float m;
}j;
int c;
void fun1()
{
int a=0,b=5;
printf("tis is fun1");
printf("\n%d%d\n",a,b);
}
//2.c--
#include<stdio.h>
struct k
{
int id;
float m;
}j;
void fun2()
{
int a=10,b=4;
printf("this is fun2()");
print开发者_如何转开发f("\n%d%d\n",a,b);
}
I compiled this code by using cc a.c 1.c 2.c
but I am getting error as storage size of ‘j’ isn’t known
//a.h---
#include<stdio.h>
#include "1.h"//cannot know its there without including it first.
#include "2.h"
extern struct k;// don't really need to do this and is wrong.
extern int c;
//a.c
int main()
{
extern int a,b;//externs i believe should be in the h file?
fun1();
fun2();
c=10;
printf("%d\n",c);
struct k *ptr = malloc(sizeof(struct k));//Define our pointer to the struct and make use of malloc.
//now we can point to the struct, update it and even retrieve.
ptr->id = 89;
ptr->m = 43;
printf("\n%d\t%f" ptr->id,ptr->m);
}
//1.h
#include<stdio.h>
typeof struct k
{
int id;
float m;
}j;
//1.c
int c;
void fun1()
{
int a=0,b=5;
printf("tis is fun1");
printf("\n%d%d\n",a,b);
}
//2.h--
#include<stdio.h>
struct k
{
int id;
float m;
}j;
//2.c
void fun2()
{
int a=10,b=4;
printf("this is fun2()");
printf("\n%d%d\n",a,b);
}
I have edited your code in places so it should see the struct and point to it. Each C file should know have an header h file. When the a.h belonging to your main includes files not only it can see them but should be able to access them. This means it should also know what K is J is alias of K if I remember correctly.
I should know update the struct and retrieve data from it via pointer. If this still doesn't work please post your compiling error and copy n paste the line its crying about.
You have no definition for struct k
visible from a.c
. Put the definition in a header file and include it from all three source files.
struct k
is a description of how to build an object. It is not an object. extern
operates on objects.
Usually struct
blocks are placed in a header, or .h
file.
j
is an object of type k
. It should be declared extern
in either 1.c
or 2.c
.
Functions which are used among multiple files, like variables, should be declared before use.
Putting it all together, you might want
//a.c---
#include <stdio.h>
#include "k.h" /* contains definition of struct k */
extern int c;
extern k j;
extern void fun1();
extern void fun2();
int main()
{
extern int a,b;
fun1();
fun2();
c=10;
printf("%d\n",c);
j.id=89;
j.m=43;
printf("\n%d\t%f",j.id,j.m);
}
Some of your concepts are wrong. Please check this link.
To access a variable as extern you have to first define it, which you have not done in your case.
The extern
modifier changes the linkage of the variable that you are declaring or defining. In C, only variable names can have linkage - not types, like struct k
.
If you wish to access the internals of a struct
type, it must be fully defined in the same source file. The usual way to accomplish this for types that are shared between source files is to place the definition of the struct
into a header file, which is included with #include
into each source file.
精彩评论