Networker
Networker

Reputation: 1

How to initialize and allocate memory for an array in a struct in header file?

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]);
     /* ... */
}

Upvotes: 0

Views: 294

Answers (2)

rwols
rwols

Reputation: 3078

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

Upvotes: 0

Puppy
Puppy

Reputation: 146910

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.

Upvotes: 3

Related Questions