?
Solved

How to use structures globally

Posted on 2003-03-05
7
Medium Priority
?
239 Views
Last Modified: 2010-04-15
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
Comment
Question by:srikanthbairi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8076600
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
 
LVL 11

Expert Comment

by:dimitry
ID: 8076675
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
 
LVL 5

Accepted Solution

by:
Kocil earned 200 total points
ID: 8077173
// *** 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
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.

 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8078279
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
 

Author Comment

by:srikanthbairi
ID: 8079916
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
 
LVL 6

Expert Comment

by:gj62
ID: 8080251
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
 

Author Comment

by:srikanthbairi
ID: 8092742
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question