Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct in C++

Status
Not open for further replies.

youngun

Programmer
Apr 27, 2001
61
US
If I have the following structure

struct Time {
int hour;
int minute;
};

and in main, I declared

Time timeArray[10][10];

How do I access the element?
can I do.....
timeArray[1][1].hour = 0; ?
but I get a compile error?
Thank you
 
You should not get any comiple time error. The following compiled and linked ok ->

#include <iostream>

struct Time {

int iHour;
int iMin;

};

int main()
{

Time timeArray[10][10];

timeArray[1][1].iMin = 0;
timeArray[1][1].iHour = 1;

std::cout << timeArray[1][1].iMin <<
std::endl;
std::cout << timeArray[1][1].iHour <<
std::endl;

return 0;
}


Perhaps your saving your source file with a .c extension? This could cause a compile time error. Try renaming your source with .cpp. Mike L.G.
mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top