• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

passing string global variable

Hi All,
  I am sure this is an easy one, but for some reason, I can't figure it out.  I am trying to set up a global variable string in a header file, initialize the value in main.c and then access the variable in a function in another .c file.  Whenever I try to access from that secondary file, I get nothing.  Here is a simpified example below that I hope will show what I am trying to do.


/* variables.h */

char  filename[30];


/* main.c */

#include "variable.h"

strcpy(filename, "filename.dat");
print_filename();

/* other.c */

#include "variable.h"

print_filename()
{
   printf("%s", filename);
}


For some reason, the value of filename is always NULL in the other.c app.  Why is this? What am I doing wrong?  Thanks.
0
drewman75
Asked:
drewman75
1 Solution
 
brettmjohnsonCommented:
Common global storage deprecated in ANSI C.
What you now have is two separate arrays called filename, one in each of your .o files.
You need to define the array in only one of the .c files, and declare it extern in the .h file:

/* variables.h */
extern char * filename;


/* main.c */

#include "variable.h"

char  filename[30];
...
strcpy(filename, "filename.dat");
print_filename();

/* other.c */

#include "variable.h"

print_filename()
{
   printf("%s", filename);
}
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi drewman75,

Note that some (particularly) older compilers don't allow you to declare a variable as "extern" (as Brett suggests) and initialize it (give it a default value) in the same object.

Otherwise, Brett's 100% right.  He's like that.  :)


Good Luck!
Kent
0
 
PaulCaswellCommented:
Hi brettmjohnson,

Isnt:

extern char filename[30];

a little better? I know it used to be not allowed but it seems to work nowadays on compilers I use.

Paul
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
cryptosidCommented:
I thought the extern declaration should be in main.c and other files that are going to share the common variable and not in the variables.h, the variables.h should contain the global declaration.


correct me if i am wrong..

Regards,
Siddhesh
0
 
PaulCaswellCommented:
What is happening here is the actual data is in the 'main' module (sort-of) and the header is telling all the other modules that it exists and that the linker will be told about its final location so they have to register all references to it for the linker to access at final build time.

The style you mentioned would create a new instance for every module that includes the header.

Paul
0
 
cryptosidCommented:
Hi PaulCaswell,

Ah! I overlooked the 'purpose' the program is being written..yes you are correct.


Regards,
Siddhesh

0
 
winsoftechCommented:
try this one

#include "variable.h"

/*main.c*/

int main()
{
 strcpy(filename, "filename.dat");
 #include "other.h"

}
/* other.h */
 printf("%s", filename);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

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