1

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()");
  printf("\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

Itay Karo
  • 17,924
  • 4
  • 40
  • 58
vaishnavi
  • 21
  • 1
  • 1
  • 2

5 Answers5

2
//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.

James Smith
  • 73
  • 10
0

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

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);
}
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • in the code in the question, j is a local variable in main. You have changed it into a global variable (and not defined it). extern doesn't reserve any storage, it just tells the compiler that there is a j somewhere. – JeremyP Aug 26 '10 at 07:44
  • @Jeremy: Since `j` is a defined as a global in the other files, and `k` was declared `extern`, I assume this is what he wants. Obviously this file is supposed to be compiled with the others. Anyway, it looks like he's just playing around. – Potatoswatter Aug 26 '10 at 07:51
0

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.

Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69
-1

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.

caf
  • 233,326
  • 40
  • 323
  • 462