0

My question is like this, there is a struct data in test.h

struct data {
static int year[10];
static int month[12];
static int day[31];
};

In Cpp file, I have several functions which need to call it. Where and how should I initialize it?

void test::display(){
     struct data pointer;
     /* ... */
     // These three arrays should be initialized
     pointer.year[index1] = Timeyear;
     pointer.month[index2] = Timemonth;
     pointer.day[index3] = Timeday;
     printf("%d %d %d", pointer.year[index1],
         pointer.month[index2], pointer.day[index3]);
     /* ... */
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273

2 Answers2

3

You appear to be very confused between C and C++.

printf? It's bad. Really bad. struct data pointer? Ditch the struct.

Also, if you want to initialize it, just use a constructor.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I put "int data::year[] = 0;" into the constructor, but it has an error "invalid use of qualified-name ‘data::year’" when compile it. – Networker Jan 18 '12 at 03:10
0

I think your problem has already been taken care of by the STL.

You should learn more about std::time_t objects

See also:

String representation of time_t?

How to get a local date-time from a time_t with boost::date_time?

time_t conversion format question

Community
  • 1
  • 1
rwols
  • 2,968
  • 2
  • 19
  • 26