-4

I'm try to make an payroll program in c++. In the beginning of my program I have to define a struct called EmployeeT that will store all the information about an employee together in one unit.

Then I have to take all that information and put it in an array of EmployeeT structures called employees.

I have this so far...

typedef struct 
{
  char name[];
  char title;
  double gross;
  double tax;
  double net;
}  EmployeeT;

So, What am I miss or doing wrong?

Thanks guys

1
  • in C++ typedef enabled by default.why char name[] ?, it's very dangerous or resizing of struct. Structure should be stable size. Aug 14, 2012 at 18:59

4 Answers 4

3

What you have there is fine, except that if you want a flexible array member (char name[]), it needs to be the last field in the structure. Probably what you really want is a pointer (char *name) or a real array (char name[SOME_SIZE]), though.

4
  • Oh ok I think I understand. How would I now take that information and put into an array of EmployeeT structures?
    – Gvegas222
    Feb 1, 2012 at 3:11
  • How would you make an array of anything else?
    – Carl Norum
    Feb 1, 2012 at 3:17
  • Is it something like... employees[EmployeeT]; ?
    – Gvegas222
    Feb 1, 2012 at 3:20
  • No. Would you make an array of integers using array[int]?
    – Carl Norum
    Feb 1, 2012 at 15:28
2

When you declare char name[], you need to give it a length for the array if it is going to be static. Otherwise, declare it as a pointer so that you can dynamically create the array later.

The struct needs to know exactly how large it will be, so you can't have an array with unknown size inside it.

2

change char name[] to char name* .

when initializing use:

name = new name[SIZEOFARRAY];

and, when you dont need it anymore, dont forget to delete it:

delete [] name;
1
  • 1
    Careful! When you use new[], you must make sure to pair it with delete[]. Feb 1, 2012 at 3:15
0

for example

struct stu
{
int num;
char name[20];
char sex;
float score;
};
1
  • Oh ok I think I understand. How would I now take that information and put into an array of EmployeeT structures?
    – Gvegas222
    Feb 1, 2012 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.