| | |
Multiple files C project.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Solved Threads: 0
Hi all,
I have a C project consisting of five files as follows:
main.c
file1.c
file1.h
file2.c
file2.h
I have defined an array in file file1.h as follows:
the array was populated in file file1.c
Now I need to use the contents of this array again in file file2.c , I don't get a compilation error when i try to print the cotents of the array in file2.c, but at run time I get nothing as if the code is never encountered. So I tried the following two approaches:
1- I changed the definition of the array in file1.h to:
This time I got a compilation error generated by references to the array in file file1.c, the error message stated that the array is unknown. Why?
so I tried this:
2- I kept the definition in file1.h as:
and added the following line in file2.c:
the project compiles but at run time the array seems to be empty.
My question is:
The array is populated in file1.c, and I want to reference its contents in , how can I do that? cause I really don't think that answer is to repopulate it in file2.c! or is it?!
Thanks in advance for your help.
I have a C project consisting of five files as follows:
main.c
file1.c
file1.h
file2.c
file2.h
I have defined an array in file file1.h as follows:
C++ Syntax (Toggle Plain Text)
static struct prnc keys[MAXP];
Now I need to use the contents of this array again in file file2.c , I don't get a compilation error when i try to print the cotents of the array in file2.c, but at run time I get nothing as if the code is never encountered. So I tried the following two approaches:
1- I changed the definition of the array in file1.h to:
C++ Syntax (Toggle Plain Text)
extern struct prnc keys[MAXP];
so I tried this:
2- I kept the definition in file1.h as:
C++ Syntax (Toggle Plain Text)
static struct prnc keys[MAXP];
C++ Syntax (Toggle Plain Text)
extern struct prnc keys[MAXP];
My question is:
The array is populated in file1.c, and I want to reference its contents in
C++ Syntax (Toggle Plain Text)
file2.c
Thanks in advance for your help.
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Solved Threads: 0
Ok check this out,
I defined the array in file1.h as:
then, I included the following in file2.c:
and it worked!
Please let me know if you think this is a poor scheme or that I should go about this in another way.
But either way I really appreciate your help, Thank you : )
I defined the array in file1.h as:
C++ Syntax (Toggle Plain Text)
struct prnc keys[MAXP];
then, I included the following in file2.c:
C++ Syntax (Toggle Plain Text)
extern struct prnc keys[MAXP];
and it worked!
Please let me know if you think this is a poor scheme or that I should go about this in another way.
But either way I really appreciate your help, Thank you : )
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Solved Threads: 0
But I want to know the proper way if you can kindly tell me,
file1.c has the following header files:
file2.c has the following header files:
But applying the scheme you're suggesting -which is approach #1 in my first post- does not work.
file1.c has the following header files:
C++ Syntax (Toggle Plain Text)
#include "file1.h"
file2.c has the following header files:
C++ Syntax (Toggle Plain Text)
#include "file1.h" #include "file2.h"
But applying the scheme you're suggesting -which is approach #1 in my first post- does not work.
![]() |
Similar Threads
- parsing multiple files in current directory (Perl)
- problems writing multiple files (Perl)
- How to Rename Multiple Files with Windows Explorer (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Deleting a pointer?
- Next Thread: C++ Help with enum and cin
Views: 3029 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






