Hello everybody!
I am using Dev C++ v4.9.7.0 on Windows ME &
I have the following problem:
I have created a database, where the data have to lie on a different file than the main program.
1. In my main program, I have created the
following structure:
struct data{
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
char ownGender; /* m or f */
int age;
char city[MAX_NAME_LENGTH];
char prefGender; /* m or f or x (for "don't care") */
int prefAgeMin;
int prefAgeMax;
};
int main() /* our main program starts here */
{
extern void info();
extern struct data person1;
extern struct data person2;
extern struct data person3;
extern struct data person4;
extern struct data person5;
info(); /* here we call the external function, so */
/* that the data for each person are */
/* inserted in our database */
.... blah ... blah ... blah ...
}
2. The data file, which is a separate file from the
main program, looks like this:
When I run this program, I get the following error:
--- storage size of 'person' isn't known ---
I guess that I have to allocate some storage room to the person[5] variable, but I do not know how to do it.
Do I use a statement like: person[5] malloc();
Does anybody have any idea?
Please help, I am a beginner.
I just do not know how to do it!
Do I put it on the main program file, or the external file that contains the database info?
Is it any header I am missing, like
#include <string.h> ? I have no clue...
Thanks in advance!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If I'm not misstaken the extern struct data person1; should go into the file where info() resides, because there you want to tell the compiler that there is some extern type you defined elsewhere.
If you switch to c++ use new and delete for dynamically allocation memory. In C it's malloc() and free(). To allocate memory for let's say 6 structs use
struct data *pointer = malloc(sizeof(data)*5);
Be reminded that when working with dynamic memory you will have to work with pointers.
upcase
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-02-12
Each module is separately compiled. When you comple the file containing info(), it knows nothing about the structure of struct data.
You need to put
struct data {...} ;
In a header file, say "person.h", and #include it in both source files.
#include "person.h"
for example. Using "" instead of <> means it will search the current directory for the header rather that the include paths.
There are many errors also. the person[] array is local to the function info(), and will go out of scope, and be destroyed when infor exits, so info() effectively does nothing.
There is no need for the function info() at all. Just initialise the array outside of a function. This module nedd contain no functions at all, just data instantiation. Constant initializers are applied at runtime beforme main is called. (although the use of global data is not necessarily best practice, but lets walk before we run here!).
All your extern declarations should go. There is no person1, person2 etc. in any case, you probably meant person[1], person[2] etc. But you do not declare individual array members, but the whole array. In the the new header person.h file simply add:
extern struct data person[] ;
------------------
so person.h contains:
----------------------------
struct data{
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
char ownGender; /* m or f */
int age;
char city[MAX_NAME_LENGTH];
char prefGender; /* m or f or x (for "don't care") */
int prefAgeMin;
int prefAgeMax;
};
extern struct data person[] ;
---------------------------
That should get you started. A more flexible implementation od a data base would be to use a csv text file (comma separated values), and use file I/O to read in an initialise a dynamically allocated database. However as I said, lets walk before we can run.
Arguably splitting this simple program amongst two modules is a bit over-the-top, however it is good practice to do so, when your programs get more sophisticated you will already have gained the necessary discipline to be able to manage the code.
Clifford.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have no command line stating the 'malloc' anywhere.
Is that supposed to be a problem?
How can I solve these errors?
Can I run the program without a 'malloc'?
In a different program, the program could not let me use 'malloc' & 'free' commands - I would get
the following errors:
1. --- 'malloc' undeclared (first use this function) ---
2. --- 'free' undeclared (first use this function) ---
Is there any #include <malloc.h> command?
& #include <free.h> ?
How do I solve these errors?
Can I e-mail somebody the code, to take a look?
It is a very simple program...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-02-12
Sorry, in the text I explained that person.h needed to be included in both source files, but in the code I omitted it.
malloc() and free() require <stdlib.h>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everybody!
I am using Dev C++ v4.9.7.0 on Windows ME &
I have the following problem:
I have created a database, where the data have to lie on a different file than the main program.
1. In my main program, I have created the
following structure:
struct data{
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
char ownGender; /* m or f */
int age;
char city[MAX_NAME_LENGTH];
char prefGender; /* m or f or x (for "don't care") */
int prefAgeMin;
int prefAgeMax;
};
int main() /* our main program starts here */
{
extern void info();
extern struct data person1;
extern struct data person2;
extern struct data person3;
extern struct data person4;
extern struct data person5;
info(); /* here we call the external function, so */
/* that the data for each person are */
/* inserted in our database */
.... blah ... blah ... blah ...
}
2. The data file, which is a separate file from the
main program, looks like this:
void info()
{
struct data person[5] =
{
{"tom","hanks",'m',20,"athens",'f',16,25},
{"nick","stalion",'m',20,"paris",'f',20,40},
{"john","lukas",'m',43,"boston",'m',20,35},
{"lora","norson",'f',23,"monaco",'f',22,33},
{"tiffany","Wood",'f',22,"texas",'m',25,40}
} ;
The problem that I am facing is this:
When I run this program, I get the following error:
--- storage size of 'person' isn't known ---
I guess that I have to allocate some storage room to the person[5] variable, but I do not know how to do it.
Do I use a statement like: person[5] malloc();
Does anybody have any idea?
Please help, I am a beginner.
I just do not know how to do it!
Do I put it on the main program file, or the external file that contains the database info?
Is it any header I am missing, like
#include <string.h> ? I have no clue...
Thanks in advance!
If I'm not misstaken the extern struct data person1; should go into the file where info() resides, because there you want to tell the compiler that there is some extern type you defined elsewhere.
If you switch to c++ use new and delete for dynamically allocation memory. In C it's malloc() and free(). To allocate memory for let's say 6 structs use
struct data *pointer = malloc(sizeof(data)*5);
Be reminded that when working with dynamic memory you will have to work with pointers.
upcase
Each module is separately compiled. When you comple the file containing info(), it knows nothing about the structure of struct data.
You need to put
struct data {...} ;
In a header file, say "person.h", and #include it in both source files.
#include "person.h"
for example. Using "" instead of <> means it will search the current directory for the header rather that the include paths.
There are many errors also. the person[] array is local to the function info(), and will go out of scope, and be destroyed when infor exits, so info() effectively does nothing.
There is no need for the function info() at all. Just initialise the array outside of a function. This module nedd contain no functions at all, just data instantiation. Constant initializers are applied at runtime beforme main is called. (although the use of global data is not necessarily best practice, but lets walk before we run here!).
All your extern declarations should go. There is no person1, person2 etc. in any case, you probably meant person[1], person[2] etc. But you do not declare individual array members, but the whole array. In the the new header person.h file simply add:
extern struct data person[] ;
------------------
so person.h contains:
----------------------------
struct data{
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
char ownGender; /* m or f */
int age;
char city[MAX_NAME_LENGTH];
char prefGender; /* m or f or x (for "don't care") */
int prefAgeMin;
int prefAgeMax;
};
extern struct data person[] ;
---------------------------
main.c contains
---------------------------
#include "person.h"
int main() /* our main program starts here */
{
.... blah ... blah ... blah ...
}
person.c contains
---------------------------
struct data person[5] =
{
{"tom","hanks",'m',20,"athens",'f',16,25},
{"nick","stalion",'m',20,"paris",'f',20,40},
{"john","lukas",'m',43,"boston",'m',20,35},
{"lora","norson",'f',23,"monaco",'f',22,33},
{"tiffany","Wood",'f',22,"texas",'m',25,40}
} ;
-----------------------------
That should get you started. A more flexible implementation od a data base would be to use a csv text file (comma separated values), and use file I/O to read in an initialise a dynamically allocated database. However as I said, lets walk before we can run.
Arguably splitting this simple program amongst two modules is a bit over-the-top, however it is good practice to do so, when your programs get more sophisticated you will already have gained the necessary discipline to be able to manage the code.
Clifford.
I am using Dev C++ v4.9.7.0 on Windows ME
Dear Mr. Clifford Slocombe,
I did everything exacltly as you said, but I still get an error:
----- storage size of 'person' isn't known -----
The error is supposed to be in the 2 bottom places:
on person.c
---------------------------------------
error 1 ---> struct data person[5] =
{
{"tom","hanks",'m',20,"athens",'f',16,25},
{"nick","stalion",'m',20,"paris",'f',20,40},
{"john","lukas",'m',43,"boston",'m',20,35},
{"lora","norson",'f',23,"monaco",'f',22,33},
{"tiffany","Wood",'f',22,"texas",'m',25,40}
error 2 ---> } ;
I have no command line stating the 'malloc' anywhere.
Is that supposed to be a problem?
How can I solve these errors?
Can I run the program without a 'malloc'?
In a different program, the program could not let me use 'malloc' & 'free' commands - I would get
the following errors:
1. --- 'malloc' undeclared (first use this function) ---
2. --- 'free' undeclared (first use this function) ---
Is there any #include <malloc.h> command?
& #include <free.h> ?
How do I solve these errors?
Can I e-mail somebody the code, to take a look?
It is a very simple program...
Sorry, in the text I explained that person.h needed to be included in both source files, but in the code I omitted it.
malloc() and free() require <stdlib.h>