| | |
Global Variables help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2004
Posts: 140
Reputation:
Solved Threads: 2
Suppose i have a main.cpp file and a test.h file:
///////////////////////////////////////////
//main.cpp
#include <iostream.h>
#include <string.h>
void test();
char foo[200];
int main()
{
strcpy(foo, "testing");
test();
return 0;
}
//////////////////////////////////////////////////////////////
//test.h
void test()
{
cout << mir << endl;
}
WHY IS "mir" AN UNDECLARED IDENTIFIER
///////////////////////////////////////////
//main.cpp
#include <iostream.h>
#include <string.h>
void test();
char foo[200];
int main()
{
strcpy(foo, "testing");
test();
return 0;
}
//////////////////////////////////////////////////////////////
//test.h
void test()
{
cout << mir << endl;
}
WHY IS "mir" AN UNDECLARED IDENTIFIER
Want a new website for free:
http://indigo.kjots.com/
http://indigo.kjots.com/
•
•
Join Date: Jul 2005
Posts: 55
Reputation:
Solved Threads: 0
And unless i'm mistaken, you forgot
C Syntax (Toggle Plain Text)
#include "test.h"
•
•
Join Date: Aug 2004
Posts: 140
Reputation:
Solved Threads: 2
Am sorry but i left many mistakes, undertand me, i typed this quickly without passing tru a compiler. I WAS RUNNING OUT OF TIME
HERE IS THE CORRECTED CODE:
Suppose i have a main.cpp file and a test.h file:
<< moderator edit: added [code][/code] tags >>
HERE IS THE CORRECTED CODE:
Suppose i have a main.cpp file and a test.h file:
C Syntax (Toggle Plain Text)
/////////////////////////////////////////// //main.cpp #include <iostream.h> #include <string.h> #include "test.h" void test(); char mir[200]; int main() { strcpy(mir, "testing"); test(); return 0; }
C Syntax (Toggle Plain Text)
////////////////////////////////////////////////////////////// //test.h void test() { cout << mir << endl; }
Want a new website for free:
http://indigo.kjots.com/
http://indigo.kjots.com/
•
•
Join Date: Jul 2005
Posts: 55
Reputation:
Solved Threads: 0
You are still not declaring mir in the test.h file. When you are using a variable in C and it is declared in a different file you have to declare it again using the "extern" keyword to let the compiler know that the variable is declared else where, so try...
main.c (using C code instead of C++)
test.h
My compiler was complaining about my iostream.h file so i just used the printf instead and omitted the need for string.h but initializing the char array when i declared it.
main.c (using C code instead of C++)
C Syntax (Toggle Plain Text)
#include "test.h" char mir[200]="testing"; int main() { test(); return 0; }
C Syntax (Toggle Plain Text)
void test() { extern char mir[200]; printf("%s\n, mir); }
My compiler was complaining about my iostream.h file so i just used the printf instead and omitted the need for string.h but initializing the char array when i declared it.
![]() |
Similar Threads
- Username/Password help, OrderBy help, and Global variables (Visual Basic 4 / 5 / 6)
- C- Syntax to allocate Global variables to consecutive memory locations (C)
- Global Variables in Functions (Python)
- Global Variables not so 'global' (Visual Basic 4 / 5 / 6)
- Global Variables (C++)
- Global v/s Public (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: problem again
- Next Thread: pls i need help
Views: 4021 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h





