Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to use structures globally

Hi,
Can any one help me tell how to use the structures globally.

I have the following code:

// **** Main.c Program ****
#include <stdio.h>
#include <pthread.h>
#include "FiletoInclude.h"
typedef struct MyStructure {
   int a;
   float b;
}

MyStructure Glob;

int main(void) {
   Glob.a = 1;
   Glob.b = 1.0;

   pthread_t t;
   pthread_create(&t, NULL, Print, (void *) 0);
   pthread_join(t, NULL);

}
// **** End of Main ****


// *** FiletoInclude.h ***
#include <stdio.h>
extern void* Print(void * );
extern struct MyStruct Glob;
// *** End of FiletoInclude.h ***

// **** File Print.c ****
#include <stdio.h>
#include "FiletoInclude.h"

void* Print(void* args) {
   extern struct MyStructure Glob;
   printf("The value of a in Glob is %d\n", Glob.a);
   printf("The valud of b in Glob is %f\n", Glob.b);
   return NULL;
}

I am getting an error saying that its a forward declaration. How can I use the structure Glob and print its values using Print function. Not only that, how can I update Glob from Print. Any ideas would be welcome. Thanks.
0
srikanthbairi
Asked:
srikanthbairi
1 Solution
 
gj62Commented:
Put the typedef in your include file that gets included in both modules.  Then, pass the struct, or a pointer to the struct, as an arguement in your print function.  You don't need to declare it as extern at all that way...
0
 
dimitryCommented:
Try to use in sme H file:
typedef struct {
  int a;
  float b;
} MyStructure;
extern MyStructure Glob;

So you will not need to use struct MyStructure but
MyStructure Glob;

Then in any file you want to use Glob inlude this H file.
0
 
KocilCommented:
// *** FiletoInclude.h ***
extern void* Print(void * );
typedef struct MyStructure {
  int a;
  float b;
}
extern struct MyStruct Glob;
// *** End of FiletoInclude.h ***

// **** File Print.c ****
#include <stdio.h>
#include "FiletoInclude.h"

void* Print(void* args) {
  printf("The value of a in Glob is %d\n", Glob.a);
  printf("The valud of b in Glob is %f\n", Glob.b);
  return NULL;
}

// **** Main.c Program ****
#include <stdio.h>
#include <pthread.h>
#include "FiletoInclude.h"

MyStructure Glob;

int main(void) {
  Glob.a = 1;
  Glob.b = 1.0;

  pthread_t t;
  pthread_create(&t, NULL, Print, (void *) 0);
  pthread_join(t, NULL);

}
// **** End of Main ****

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sarda_rameshCommented:
The solution provided by kocil looks fine but the File_Print.c seems to have some problem.

// **** File Print.c ****
#include <stdio.h>
#include "FiletoInclude.h"

void* Print(void* args) {
 printf("The value of a in Glob is %d\n", Glob.a);
 printf("The valud of b in Glob is %f\n", Glob.b);
 return NULL;
}


The varaible Glob is declared in Main.c and therfore thsi file will not recoginze this variable and will flash an error showing undeclared variable Glob.

I think the variable Glob must be passed as argument to the function Print or this variable must be declared in the FiletoInclude.h

Regards
Ramesh
0
 
srikanthbairiAuthor Commented:
Thanks Guys,
I found the answers given by all of you very very useful. I have a situation where some structure needs to be used by the main program and some 3 threads that it spawns. Main and the 3 threads should use the same structure and its initialization has to happen in main because main program initialized the structure after reading a configuration file or some other input. I got the best answers. Thank You All.

Regards
Srikanth Bairi
0
 
gj62Commented:
That's VERY dangerous.

If you mean that main and the 3 threads are using the same physical copy of the structure, and will be changing the values in that structure, you will either need to mutex it (which can negate the value of threading in some cases), or some up with another method of handling the issue of 2 threads attempting to change the structure at the same time.

Generally, it is a bad idea to use globals anytime you have a multi-threaded application...
0
 
srikanthbairiAuthor Commented:
Thankyou gj62. I am very much aware of this problem. I have a lot of threads that need to update a global variable. All of them should of course are controlled by a global mutex or a global semaphore so that at any one point of time, only one can update the global variable. Thanks gj62.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now